从命令行运行PHP脚本作为后台进程 [英] Running PHP script from command line as background process

查看:277
本文介绍了从命令行运行PHP脚本作为后台进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在后台通过Linux中的命令行连续运行PHP脚本。我试过命令 php filename.php& ,但似乎脚本执行终止非常快,而它应该保持运行,直到进程终止。

I'm trying to run a PHP script continually in the background via the command line in Linux. I have tried the command php filename.php & but it seems like the script execution terminates very quickly, while it should keep running until the process is terminated.

有任何建议吗?

推荐答案

您确定脚本不包含任何错误?这通常是执行终止非常快,使用 error_reporting(E_ALL) ini_set('display_errors','On')显示脚本可能有的任何错误,那么你可以使用:

Are you sure the script doesn't contain any errors? That's normally what makes "execution terminates very quickly" , use error_reporting(E_ALL) and ini_set('display_errors','On') to display any errors the script may have, then you can use :

nohup php filename.php& - > nohup运行命令,即使会话断开或用户注销。

nohup php filename.php & -> nohup runs a command even if the session is disconnected or the user logs out.

也可以使用:

ignore_user_abort(1); - >设置客户端断开是否应终止脚本执行

set_time_limit(0); - >限制最大执行时间,在这种情况下它会运行,直到进程完成或apache重新启动。

You can also use:
ignore_user_abort(1); -> Set whether a client disconnect should abort script execution
set_time_limit(0); -> Limits the maximum execution time, in this case it runs until the process finishes or apache restart.

这篇关于从命令行运行PHP脚本作为后台进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆