140822 用shell写守护进程[转载]

设想:用一个shell查看一个后台运行的php程序是否非正常退出
如果退出,利用守护进程自动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”

利用这个原理,可以实现很多后台程序的掉线问题

发表评论

电子邮件地址不会被公开。 必填项已用*标注