使用shell编写linux下的守护进程脚本的方法
本例子,共共3个程序:
代码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
(责任编辑:IT)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" |