用shell写守护进程
时间:2007-10-15 来源:zixin
如果退出,利用守护进程自动restart.
类似mysql的safe_mysqld
共3个程序
shstart.sh 负责启动
sh.sh 守护进程主程序
shstop.sh 安全退出守护进程和php程序主体
/**************************/
shstart.sh
#!/usr/local/bin/bash
/home/phpshell/sh.sh &
shpid="$!";
echo "$shpid" > /home/phpshell/sh.sid
/**************************/
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
/******************************/
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"
利用这个原理,可以实现很多后台程序的吊线问题