我创建了一个脚本,它使用psexec来调用另一个调用psexec的脚本来运行我的命令行程序。
之所以对psexec和其他脚本进行如此多的调用,只是为了让我的PHP脚本不必等到进程完成后再将其输出到浏览器。
有没有一种方法可以做到这一点而不需要使用psexec?我的psexec有问题,所以我想把它从我的程序中完全删除。
我正在运行Windows 2008
编辑:我更改了标题,我想这会是一个更准确的标题。我在php.net的exec()
页面上找到了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.
,但不知道该怎么做。
在命令行中包括重定向:
exec('program.exe > NUL')
或者你可以修改你的程序以明确地关闭标准输出,在C中这将是
CloseHandle(GetStdHandle(STD_OUTPUT_HANDLE));
有可能(文档中没有说明(您可能需要重定向/关闭标准输出和标准错误:
exec('program.exe > NUL 2> NUL')
或
CloseHandle(GetStdHandle(STD_OUTPUT_HANDLE));
CloseHandle(GetStdHandle(STD_ERROR_HANDLE));
$command = 'start /B program.exe > NUL';
pclose( popen( $command, 'r' ) );
有关更多信息:http://humblecontributions.blogspot.com/2012/12/how-to-run-php-process-in-background.html
尝试Windows的"start
"命令:http://technet.microsoft.com/en-us/library/cc770297%28WS.10%29.aspx
在调用生成子进程的脚本时,使用start/B有一定的限制。以下方法生成一个子进程,使用PowerShell来处理此类情况:
function execInBackgroundWindows($filePath, $workingDirectory, $arguments)
{
$cmd = "powershell.exe Start-Process -FilePath $filePath -WorkingDirectory $workingDirectory -ArgumentList '$arguments'";
shell_exec($cmd);
}
execInBackgroundWindows('curl.exe','c:'temp','-X POST -H "Content-Type: application/json" -d @test.json http://127.0.0.1:4000/myapp');