开发者

restart a php script using shell script

i am using shell script to monitor the working of a php script. My aim is that this php script should not sleep / t开发者_开发百科erminated and must always be running.The code i used is -

ps aux | grep -v grep | grep -q $file || ( nohup php -f $file -print > /var/log/file.log & ) 

now this idea would not work for cases if the php script got terminated(process status code T). Any idea to handle that case. can such processes be killed permanently and then restarted.


How about just restarting the php interpreter when it dies?

while true ; do php -f $file -print >> /var/log/file.log ; done

Of course, someone could send the script a SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU to cause it to hang, but perhaps that person has a really good reason. You can block them all except SIGSTOP, so maybe that's alright.

Or if the script does something like call read(2) on a device or socket that will never return, this won't really ensure the 'liveness' of your script. (But then you'd use non-blocking IO to prevent this situation, so that's covered.)

Oh yes, you could also stuff it into your /etc/inittab. But I'm not giving you more than a hint about this one, because I think it is probably a bad idea.

And there are many similar tools that already exist: daemontools and Linux Heartbeat are the first two to come to mind.


If the script is exiting after it's been terminater, or if it crashes out, and needs to be restarted, a simple shell script can take care of that.

#!/bin/bash
# runScript.sh - keep a php script running
php -q -f ./cli-script.php -- $@
exec $0 $@;

The exec $0 re-runs the shell script, with the parameters it was given.

To run in the background you can nohup runScript.sh or run it via init.d scripts, upstart, runit or supervisord, among others.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜