목차
" >2. [代码]守护脚本    
php教程 PHP源码 使用Linux脚本执行定时任务

使用Linux脚本执行定时任务

May 26, 2016 am 08:20 AM


#!/bin/sh
#
#cli根目录
#以#开头的行是注释,但注意,第一行#!不是注释,是指定脚本的解释文件
clidir=/home/dev/web
#定义了一个名为clidir变量,注意=两端不能有空格

#启动短信
function start_sms()
#定义函数
{
	number=`ps -eaf | grep sendsms.sh | grep -vc grep`
	#检测守护进程是否已经运行
	#ps -eaf | grep sendsms.sh | grep -vc grep 统计包含sendsms.sh字符且不包含grep字符的进程有多少条
	#grep -vc v 显示不包含匹配文本的行 c 统计符合条件的行

	#检测是否已经开启
	if [ $number -eq 0 ]; then
	#判断守护进程的数量是否等于0
	# -eq 等于
	# 注意:数值判断和字符判断使用的操作符不一样 
	# 字符串判断
	# str1 = str2      当两个串有相同内容、长度时为真 
	# str1 != str2      当串str1和str2不等时为真 
	# -n str1         当串的长度大于0时为真(串非空) 
	# -z str1         当串的长度为0时为真(空串) 
	# str1           当串str1为非空时为真
	# 数字的判断
	# int1 -eq int2      两数相等为真 
	# int1 -ne int2      两数不等为真 
	# int1 -gt int2      int1大于int2为真 
	# int1 -ge int2      int1大于等于int2为真 
	# int1 -lt int2      int1小于int2为真 
	# int1 -le int2      int1小于等于int2为真
	# 文件的判断
	# -r file           用户可读为真 
	# -w file           用户可写为真 
	# -x file           用户可执行为真 
	# -f file           文件为正规文件为真 
	# -d file           文件为目录为真 
	# -c file           文件为字符特殊文件为真 
	# -b file           文件为块特殊文件为真 
	# -s file           文件大小非0时为真 
	# -t file           当文件描述符(默认为1)指定的设备为终端时为真
	# 逻辑判断
	# -a               与 
	# -o              或 
	# !              非

		/bin/sh ${clidir}/cli/queue/sendsms.sh & 
		#启动守护进程
		# & 符号 表示在后台运行

		echo 'service queue to sendsms start running'
	else
		echo 'service queue to sendsms already running'
	fi

	return $?;
}

#停止短信
function stop_sms()
{
	number=`ps -eaf | grep sendsms.sh | grep -vc grep`

	#检测守护进程是否已经关闭
	if [ $number -eq 0 ]; then
		echo 'service queue to sendsms already stopped'
	else
		#关闭守护进程
		for pid in $(ps -ef | grep sendsms.sh | grep -v grep | awk '{print $2}'); do
		#ps -ef | grep sendsms.sh | grep -v grep | awk '{print $2}' 逐行读取包含sendsms.sh且不包含grep的进程id
		#awk 逐行读取文本,并以空格(默认)分割每行,$1表示分割出的第一个元素,$2表示第二个... $0表示所有元素
			
			kill -9 $pid &> /dev/null
			#kill -9 $pid 杀死指定进程id的进程
		done
		echo 'service queue to sendsms has stopped'
	fi

	pid_sms=`ps -ef | grep sendsmsAction | grep -vc grep`
	if [ $pid_sms -gt 0 ]; then
		kill -9 $(ps -ef | grep sendsmsAction | grep -v grep } awk '{print $2}') &> /dev/null
	fi
	return $?
}

#查看状态
function status()
{
	echo "sendsms    :" `ps -ef | grep sendsmsAction | grep -v grep | awk '{print $2 , $8, $9, $10, $11}'`
	echo "sendemail  :" `ps -ef | grep sendemailAction | grep -v grep | awk '{print $2 , $8, $9, $10, $11}'`
	return $?
}

#查看守护进程状态
function sh_status()
{
	echo -e "sendsms    :" `ps -eaf | grep sendsms.sh | grep -v grep | awk '{print $2 , $8, $9, $10, $11}'`
	echo "sendemail   :" `ps -eaf | grep sendemail.sh | grep -v grep | awk '{print $2 , $8, $9, $10, $11}'`
	return $?
}

case "$1" in
  start)
  	if [[ "$2"  == "sendsms" ]]; then
  		start_sms
  	elif [[ "$2" == "sendemail" ]]; then
  		start_email
  	else
  		start_sms
  		start_email
  	fi
	;;
  stop)
  	if [[ "$2" == "sendsms" ]]; then
  		stop_sms
  	elif [[ "$2" == "sendemail" ]]; then
  		stop_email
  	else
		stop_sms
		stop_email
  	fi
	;;
  shstatus)
	sh_status
	;;
  status)
	status
    ;;	
  *)
	echo $"Usage: $0 {start|stop|status|shstatus}"
	exit 2
esac
exit $?
로그인 후 복사

2. [代码]守护脚本

#!/bin/bash
#守护进程脚本
#守护脚本比较简单 每隔一段时间执行一次任务 每次执行前 检测是否有还在执行的任务
selfpath=`dirname $0`
#$0 特殊变量 文件路径
cd $selfpath;
cd ../../
phpExec=/opt/app/php5/bin/php
while [[ true ]]; do
	number=`ps -ef | grep 'sendsmsAction' | grep -vc grep`
	if [ $number -eq 0 ]; then
	#每次执行任务 判断是否还有正在执行的任务
	    $phpExec   cli.php queue sendsmsAction &
	fi
	sleep 1m
	#间隔时间
done
로그인 후 복사

           


       

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

뜨거운 기사 태그

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)