在Windows上用PHP执行后台进程

时间:2011-08-07 13:23:54

标签: php background-process

我正在为Windows重写Mark's answer,到目前为止我已经想到了这个:

 // Escape character for Windows is: ^ 
 $shellCmd = 'start /B cmd /c ' . escapeshellcmd($cmd) . ' ^>"'.$outputfile. '"'; 
 // note that exec was like 40 times slower than popen & pclose 
 pclose(popen($shellCmd, "r")); 

Windows上有tasklist命令,但我不知道如何查找我的进程的PID。为了准时,我正在寻找通过popen打开的过程的PID。

你能帮帮我吗? 谢谢!

注意:我不确定这段代码的错误输出是什么,但在我的情况下并不重要。

1 个答案:

答案 0 :(得分:2)

http://php.net/manual/en/function.proc-open.php
http://php.net/manual/en/function.pcntl-fork.php
http://www.php.net/proc_get_status

阅读这些功能下的讨论,您可以更好地控制后台进程并检索其PID

像这样的例子:

$pcs = popen($shellCmd,"r");
$info = proc_get_status($pcs);
$pid = $info['pid'];
proc_close($pcs);