在linux下,借助shell查看一个后台运行的PHP程序是否非正常退出,如果退出,利用守护进程自动restart,类似mysql的safe_mysqld脚本。
其实,就是用shell脚本实现/etc/rc.d/目录的守护进程代码。
本例子,共共3个程序:
1,shstart.sh 负责启动
2,sh.sh 守护进程主程序
3,shstop.sh 安全退出守护进程和php程序主体
代码1,shstart.sh:
复制代码 代码示例:
#!/usr/local/bin/bash
/home/phpshell/sh.sh &
shpid="$!";
echo "$shpid" > /home/phpshell/sh.sid
代码2,sh.sh:
复制代码 代码示例:
#!/usr/local/bin/bash
PHP="/usr/local/bin/php"
PROGRAM="/www/time.php"
#start dameo
$PHP $PROGRAM &
chpid="$!";
echo "$chpid" > /home/phpshell/php.sid
echo "child pid is $chpid"
echo "status is $?"
while [ 1 ]
do
wait $chpid
exitstatus="$?"
echo "child pid=$chpid is gone, $exitstatus" >> /home/phpshell/phperror.log
echo `date` >> /home/phpshell/phperror.log
echo "**************************" >>/home/phpshell/phperror.log
sleep 10
$PHP $PROGRAM &
chpid="$!";
echo "$chpid" > /home/phpshell/php.sid
echo "next child pid is $chpid"
echo "next status is $?"
echo "userkill is $userkill"
done
代码3,shstop.sh:
复制代码 代码示例:
#!/usr/local/bin/bash
chpid="`cat sh.sid`";
kill $chpid;
echo "kill sh.sh done!"
ps ax|grep php |grep -v grep|awk '{print $1}'|xargs kill
sleep 2
ps ax|grep php |grep -v grep|awk '{print $1}'|xargs kill
echo "kill php done"