来自php脚本的nohup来自apache,用于长时间运行的进程

时间:2015-11-20 22:23:04

标签: php nohup

如果需要运行长时间运行的操作但是Web请求需要立即返回,那么使用exec从php脚本运行nohup是正确的方法吗?我想从发出请求后运行的php脚本中生成另一个进程。

示例:

exec ('nohup php long_running_script.php');

//Need this to return happen right away without waiting for long_running_script.php
echo json_encode(array('success' => TRUE));

1 个答案:

答案 0 :(得分:2)

如果主机上有 at 命令,我更喜欢使用:

putenv( 'SHELL=/bin/sh' );
print `echo /usr/bin/php -q full_path_to_script.php | /usr/bin/at now 2>&1`;

我会检查每个命令的路径,以确保一切都正确执行:

echo exec( 'which at' );