restart a php script using shell script

Deadly 提交于 2019-12-11 12:18:38

问题


i am using shell script to monitor the working of a php script. My aim is that this php script should not sleep / terminated 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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4780247/restart-a-php-script-using-shell-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!