在 Windows 的后台运行 PHP“exec()”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7692263/
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
Running a PHP "exec()" in the background on Windows?
提问by Rob
I've created a script that uses psexec to call another script which calls psexec to run a command line program of mine.
我创建了一个脚本,它使用 psexec 调用另一个脚本,该脚本调用 psexec 来运行我的命令行程序。
The reason for so many calls to psexec and other scripts is solely so that my PHP script doesn't have to wait for the process to finish before finishing it's output to the browser.
对 psexec 和其他脚本进行如此多调用的原因仅仅是为了让我的 PHP 脚本在完成输出到浏览器之前不必等待进程完成。
Is there a way I can do this without needing to use psexec? I'm having issues with psexec so I'd like to just completely remove it from my program.
有没有一种方法可以在不需要使用 psexec 的情况下做到这一点?我在使用 psexec 时遇到问题,所以我想将它从我的程序中完全删除。
I'm running Windows 2008
我正在运行 Windows 2008
EDIT: I changed the title, I guess this would be a more accurate title. I found out the 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.
on php.net's page on exec()
, but wasn't sure how to do that.
编辑:我更改了标题,我想这将是一个更准确的标题。我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.net 的页面上找到了exec()
,但不知道该怎么做。
回答by Harry Johnston
Include a redirection in the command line:
在命令行中包含重定向:
exec('program.exe > NUL')
or you could modify your program to explicitly close standard output, in C this would be
或者您可以修改您的程序以明确关闭标准输出,在 C 中这将是
CloseHandle(GetStdHandle(STD_OUTPUT_HANDLE));
It is possible (the documentation doesn't say) that you might need to redirect/close both the standard output and standard error:
有可能(文档没有说)您可能需要重定向/关闭标准输出和标准错误:
exec('program.exe > NUL 2> NUL')
or
或者
CloseHandle(GetStdHandle(STD_OUTPUT_HANDLE));
CloseHandle(GetStdHandle(STD_ERROR_HANDLE));
回答by Boann
Try the Windows 'start
' command: http://technet.microsoft.com/en-us/library/cc770297%28WS.10%29.aspx
尝试 Windows“ start
”命令:http: //technet.microsoft.com/en-us/library/cc770297%28WS.10%29.aspx
回答by Niko
This worked for me
这对我有用
$command = 'start /B program.exe > NUL';
pclose( popen( $command, 'r' ) );
For more info: http://humblecontributions.blogspot.com/2012/12/how-to-run-php-process-in-background.html
更多信息:http: //humblecontributions.blogspot.com/2012/12/how-to-run-php-process-in-background.html