使用 shell 脚本重新启动 php 脚本
我正在使用 shell 脚本来监视 php 脚本的工作。我的目标是这个 php 脚本不应该休眠/终止,并且必须始终运行。我使用的代码是 -
ps aux | grep -v grep | grep -q $file || ( nohup php -f $file -print > /var/log/file.log & )
现在这个想法对于 php 脚本终止的情况不起作用(进程状态代码 T)。有什么想法可以处理这个案子。可以永久终止此类进程然后重新启动吗?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果脚本在终止后退出,或者崩溃并需要重新启动,一个简单的 shell 脚本就可以解决这个问题。
exec $0
使用给定的参数重新运行 shell 脚本。要在后台运行,您可以
nohup runScript.sh
或通过 init.d 脚本、upstart、runit 或supervisord 等运行它。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.
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.当 php 解释器死掉时重新启动怎么样?
当然,有人可以向脚本发送
SIGSTOP
、SIGTSTP
、SIGTTIN
、SIGTTOU
使其挂起,但也许那个人有一个很好的理由。您可以阻止除SIGSTOP
之外的所有它们,所以也许没问题。或者,如果脚本在永远不会返回的设备或套接字上执行诸如调用 read(2) 之类的操作,则这并不能真正确保脚本的“活跃度”。 (但是你可以使用非阻塞 IO 来防止这种情况,所以这已经被涵盖了。)
哦,是的,你也可以将它填充到你的 /etc/inittab 中。但我不会给你更多关于这个的提示,因为我认为这可能是一个坏主意。
并且已经存在许多类似的工具:daemontools 和 Linux Heartbeat 是首先想到的两个。
How about just restarting the php interpreter when it dies?
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 exceptSIGSTOP
, 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.