nohup:在后台运行 PHP 进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5288584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
nohup: run PHP process in background
提问by stoe
i try to run php processes in background and start this from an php file.
我尝试在后台运行 php 进程并从 php 文件启动它。
Some informations: PHP Version 5.2.17, php safe_mode is off, linux system. I start the process with exec, tried already shell_exec. I set all files to 0755, 0777.
一些信息:PHP 5.2.17 版本,php safe_mode 关闭,linux 系统。我用 exec 开始这个过程,已经尝试过 shell_exec。我将所有文件设置为 0755、0777。
$pid = exec("nohup $cmd > /dev/null 2> /dev/null & echo $!");
If i print this statement, i get this and the pid is okay:
如果我打印此语句,我会得到这个并且 pid 没问题:
nohup /usr/local/bin/php5 /.../../file.php > /dev/null 2> /dev/null & echo $!
If i look for processes under ssh with
如果我在 ssh 下寻找进程
top
i see my php5 process with the correct pid. user is root
我看到我的 php5 进程具有正确的 pid。用户是root
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3533 xxxxxxxx 20 0 21356 8868 4580 S 0 0.4 0:00.13 php5
3536 xxxxxxxx 20 0 20836 8260 4428 S 0 0.4 0:00.09 php5
3539 xxxxxxxx 20 0 20836 8260 4428 S 0 0.4 0:00.09 php5
3542 xxxxxxxx 20 0 20836 8260 4428 S 0 0.4 0:00.09 php5
3545 xxxxxxxx 20 0 20836 8260 4428 S 0 0.4 0:00.09 php5
3548 xxxxxxxx 20 0 20836 8260 4428 S 0 0.4 0:00.09 php5
3551 xxxxxxxx 20 0 20836 8260 4428 S 0 0.4 0:00.09 php5
if i start the process manual top looks like this:
如果我开始流程手册顶部看起来像这样:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
8141 xxxxxxxx 22 2 24048 9.9m 5344 S 10 0.5 0:00.31 php5
The problem:it seems like nothing happens.To debug a little bit i wrote a output in the file
问题:似乎什么也没发生。为了调试一点,我在文件中写了一个输出
ob_start();
echo "STARTING...";
writeLog(ob_get_contents());
//...
function writeLog($info){
$handle = fopen("file.log", "a+");
fwrite($handle, $info);
fclose($handle);
}
exit;
No logs in my file. If I start this file in my browser, it processes correctly and get my file.log with the "debugging" information.
我的文件中没有日志。如果我在浏览器中启动此文件,它会正确处理并获取带有“调试”信息的 file.log。
Why this is working in browser and not on exec/shell_exec command??! i have exactly these php processes with correct pid, but no result.
为什么这在浏览器中有效而不是在 exec/shell_exec 命令上??!我有正确的pid这些php进程,但没有结果。
Thanks a lot for help!
非常感谢您的帮助!
采纳答案by Sander Marechal
The PHP CLI environment can be different from the web environment (mod_php, FCGI, whatever). It's entirely possible for a script to die with an error when run from the commandline and not when you invoke it through a webserver. Debug your script. If you can, SSH into your server, su to the webserver user and run the script from the commandline yourself.
PHP CLI 环境可以不同于 Web 环境(mod_php、FCGI 等等)。当从命令行运行而不是通过网络服务器调用它时,脚本完全有可能因错误而死亡。调试您的脚本。如果可以,通过 SSH 连接到您的服务器,su 到网络服务器用户并自己从命令行运行脚本。
If you can't do that, set up your own development server where you can do this (it's not that hard if you know some Linux).
如果您不能这样做,请设置您自己的开发服务器,您可以在其中执行此操作(如果您了解一些 Linux,这并不难)。
回答by chf
Except possible permission issue, a similar answer is posted here - https://stackoverflow.com/a/22705727/3471333.
除了可能的权限问题,这里发布了一个类似的答案 - https://stackoverflow.com/a/22705727/3471333。
nohup /usr/local/bin/php5 /.../../file.php </dev/null 2> /dev/null & echo $!