PHP exec() 作为后台进程(Windows Wampserver 环境)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5367261/
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
PHP exec() as Background Process (Windows Wampserver Environment)
提问by Emmanuel
I'm trying to setup a php trigger file that will set off a background process. (see this question)
我正在尝试设置一个 php 触发器文件,它将引发后台进程。(见这个问题)
I'm doing this on a Windows Wampserver environment.
我在 Windows Wampserver 环境中执行此操作。
So for example I have trigger.php
that runs the exec function that calls for my backgroundProcess.php
to be parsed and executed.
因此,例如我trigger.php
运行 exec 函数,该函数要求backgroundProcess.php
解析和执行我的。
However the problem is that my trigger.php
file is waiting for the exec()
command to finish running backgroundProcess.php
before it stops. The background process runs for about 20-30 seconds, and trigger.php
is waiting all that time until backgroundProcess.php
has fully finished.
但是问题是我的trigger.php
文件正在等待exec()
命令backgroundProcess.php
在停止之前完成运行。后台进程运行大约 20-30 秒,并trigger.php
一直等待直到backgroundProcess.php
完全完成。
Is that making sense? Here is the trigger.php
file that runs the exec()
command
这有道理吗?这是trigger.php
运行exec()
命令的文件
exec('C:\wamp\bin\php\php'.phpversion().'\php.exe -f C:\path\to\backgroundProcess.php > C:\wamp\bin\php\php'.phpversion().'\dev\null &');
Basically, I'm wanting trigger.php
to just trigger off the backgroundProcess and not wait around for it to finish.
基本上,我trigger.php
只想触发 backgroundProcess 而不是等待它完成。
EDIT
编辑
Problem solved with the following command:
问题通过以下命令解决:
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("C:\wamp\bin\php\phpVERSIONNUMBER\php-win.exe -f C:/wamp/www/path/to/backgroundProcess.php", 0, false);
回答by Emmanuel
Problem solved with the following command:
问题通过以下命令解决:
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("C:\wamp\bin\php\phpVERSIONNUMBER\php-win.exe -f C:/wamp/www/path/to/backgroundProcess.php", 0, false);
回答by Rohit
Tried to achieve the same on a Windows 2000 server with PHP 5.2.8.
试图在带有 PHP 5.2.8 的 Windows 2000 服务器上实现相同的目标。
None of the solutions worked for me. PHP kept waiting for the response.
没有一个解决方案对我有用。PHP 一直在等待响应。
Found the solution to be :
发现解决方案是:
$cmd = "E:\PHP_folder_path\php.exe E:\some_folder_path\backgroundProcess.php";
pclose(popen("start /B ". $cmd, "a")); // mode = "a" since I had some logs to edit
ps : Posting the same reply to the other thread (PHP on a windows machine; Start process in background) since these 2 links helped me a lot in doing some research on this.
ps:向另一个线程(Windows 机器上的 PHP;在后台启动进程)发布相同的回复,因为这 2 个链接帮助我对此进行了一些研究。
回答by JohnP
From the manual : http://www.php.net/manual/en/function.exec.php
从手册:http: //www.php.net/manual/en/function.exec.php
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 挂起,直到程序执行结束。
And a similar question I answered : Call another PHP script and return control to user before the other script completes
我回答了一个类似的问题:调用另一个 PHP 脚本并在另一个脚本完成之前将控制权返回给用户
回答by Shamim Hafiz
You may need to change your implementation approach. Having to wait for such a long time would be an annoyance for the user of your app and fatal for the entire app.
您可能需要更改您的实施方法。等待这么长时间对您的应用程序用户来说是一种烦恼,对整个应用程序来说都是致命的。
For such tasks, it's usually better to queue the task, ideally on database, and process them periodically. There are chron jobs on Linux based systems. In Windows, you can use a scheduler to launch the backgroundProcess.php.
对于此类任务,通常最好将任务排队,最好是在数据库上,并定期处理它们。在基于 Linux 的系统上有 chron 作业。在 Windows 中,您可以使用调度程序来启动 backgroundProcess.php。
回答by John
It may well be that when using the exec()
in a windows environment that redirection is to NUL:
. /dev/null
is a *nix null file.
很可能exec()
在 Windows 环境中使用时,重定向是到NUL:
. /dev/null
是一个 *nix 空文件。
回答by William N Irwin
In addition to Rohit's answer above, I edited his solution to work on Windows 10 PHP 7.0.3:
除了上面 Rohit 的回答之外,我还编辑了他的解决方案以在 Windows 10 PHP 7.0.3 上工作:
pclose(popen("start /B ". $cmd, "w"));