php exec() 后台进程问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14555971/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:31:22  来源:igfitidea点击:

php exec() background process issues

phpexec

提问by kylex

I'm trying to process a file in the background with the following command, but it does nothing.

我正在尝试使用以下命令在后台处理文件,但它什么也没做。

exec("php csv.php $file $user > /dev/null &", $output);

If I remove > /dev/null &then the file processes, but not in the background.

如果我删除,> /dev/null &则文件会处理,但不会在后台处理。

exec("php csv.php $file $user", $output);

Any ideas?

有任何想法吗?

回答by Emery King

Note:

笔记:

If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

如果一个程序是用这个函数启动的,为了让它在后台继续运行,程序的输出必须重定向到一个文件或另一个输出流。如果不这样做,将导致 PHP 挂起,直到程序执行结束。

http://php.net/manual/en/function.exec.php

http://php.net/manual/en/function.exec.php

so:

所以:

exec("php csv.php $file $user > /dev/null &"); // no $output

回答by moodboom

Have you considered using screen? You can start up a screen session that runs in a detached process. Output will go to the screen session, which you can reattach to in another terminal while it is still running.

您是否考虑过使用屏幕?您可以启动在分离进程中运行的屏幕会话。输出将转到屏幕会话,您可以在另一个终端仍在运行时重新附加到该会话。

exec("screen -d -m -S my_php_session csv.php $file $user", $output);