PHP头条
热点:

使用Linux脚本执行定时任务-PHP源码


#!/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

www.phpzy.comtrue/phpyy/41369.htmlTechArticle使用Linux脚本执行定时任务-PHP源码 #!/bin/sh##cli根目录#以#开头的行是注释,但注意,第一行#!不是注释,是指定脚本的解释文件clidir=/home/dev/web#定义了一个名为clidir变量,注意=两端不能...

相关文章

PHP之友评论

今天推荐