php - 从命令行运行 PHP 脚本作为后台进程

标签 php linux command-line

我正在尝试通过 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.


#注释 phpfilename.php 路径可以作为 full-path 提供,而不是 phpfilename.php,可以使用/usr/bin/php/full/path/to/filename.php
建议使用完整路径以避免找不到文件错误。

关于php - 从命令行运行 PHP 脚本作为后台进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6989161/

相关文章:

Linux:命令使文件夹递归可写而不影响其中文件的权限

php - API平台: Can't get using a json data type with MySQL to work

javascript - 没有 Javascript 的动态网站内容?

linux - DRBD - 裂脑问题

c - 如何将 malloc 与 madvise 一起使用并启用 MADV_DONTDUMP 选项

linux - 找到所有具有特定扩展名的文件然后执行

PHP Javascript/CSS 打包工具(如 Haste/Juicer)

php pdo使用函数插入数据

c - 提高 MacOS X 上的 mmap/munmap 性能

linux - 可以用SAS执行Linux命令吗?