我正在尝试通过 Linux 中的命令行在后台持续运行 PHP 脚本。我已经尝试了命令 php filename.php &
但似乎脚本执行很快终止,而它应该一直运行直到进程终止。
有什么建议吗?
最佳答案
您确定脚本不包含任何错误吗?这就是通常导致“执行很快终止”的原因。
首先,追加:
error_reporting(E_ALL); ini_set('display_errors', 1);
在脚本的顶部显示它可能存在的任何错误,然后您可以使用:
nohup php 文件名.php &
nohup runs a command even if the session is disconnected or the user logs out.
或
nohup php 文件名.php >/dev/null 2>&1 &
Same as above but doesn't create
nohup.out
file.
您还可以使用:
ignore_user_abort(1);
Set whether a client disconnect should abort script execution
`set_time_limit(0);`
Limits the script maximum execution time, in this case it will run until the process finishes or the apache process restarts.
#注释
php
和 filename.php
路径可以作为 full-path 提供,而不是 php
和 filename.php
,可以使用/usr/bin/php
和/full/path/to/filename.php
。
建议使用完整路径以避免找不到文件错误。
关于php - 从命令行运行 PHP 脚本作为后台进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6989161/