shell脚本 (文件名:my_exec.sh)php
############# 配置项 ###############################shell
keyword='a.php' #查找进程是否存在的关键词
line=`ps aux | grep ${keyword} | wc -l` # 查看进程是否存在的命令
commend='php a.php' # nohup 执行的commend; 不要带 输出重定向 和 & !!!!!!
output_file='1.txt' #输出重定向文件
pid_file='exec_pid.pid' #pid文件 确保惟一且文件夹可写rest
############ 配置项 end ############################进程
###检查状态
function check_running(){
if [[ $line -lt 2 ]] ## 去除 gerp 时产生的进程
then
echo "未运行" ##### 未执行的操做
else
echo "正在运行" ##### 已执行的操做
fi
}io
function start_running(){function
if [[ $line -lt 2 ]]
then
echo "正在开始。。。。"
eval "nohup ${commend} >>${output_file} 2>&1 &"
echo $! > ${pid_file} #### 保存pid 文件
sleep 2
echo "成功"
else
echo "程序已经运行,请不要重复运行"后台
fi
}配置
function stop_running(){
if [[ $line -lt 2 ]]
then
echo "程序未运行"
elif [ ! -f "$pid_file" ]
then
echo "pid文件不存在,请使用 kill 删除进程"
else
echo "正在中止。。。。"
kill -9 `cat ${pid_file}` ##### 本身的中止方法 尽可能不要用 kill -9
rm -f $pid_file
sleep 2
echo "成功"
fidate
}
case $1 in
start):
start_running;
;;
check):
check_running;
;;
stop):
stop_running;
;;
restart):
stop_running;
start_running;
;;
*):
echo "请输入命令: sh start_exec.sh【start|stop|restart|check】"file
;;
esac
php脚本 (本身的后台脚本,必定要是可以长久运行的)
<?php
while(true){
sleep(1);
file_put_contents('1.txt',date('Y-m-d H:i:s')."\r\n",FILE_APPEND);
}
?>
方法 sh shart_exec.sh (start | stop | restart | check)