我一直用:
$pid = exec("/usr/local/bin/php file.php $args > /dev/null & echo \$!");
但我正在使用 XP 虚拟机开发 Web 应用程序,我不知道如何在 Windows 中获取 pid。
我在 cmd 上试过这个:
C:\\wamp\\bin\\php\\php5.2.9-2\\php.exe "file.php args" > NUL & echo $!
它会执行文件,但输出是“$!”
如何将 pid 放入 var $pid 中? (使用 PHP)
最佳答案
我正在使用 Pstools它允许您在后台创建一个进程并捕获它的 pid:
// use psexec to start in background, pipe stderr to stdout to capture pid
exec("psexec -d $command 2>&1", $output);
// capture pid on the 6th line
preg_match('/ID (\d+)/', $output[5], $matches);
$pid = $matches[1];
这有点hacky,但它完成了工作
关于php - 如何从 Windows 中的 PHP 函数 exec() 获取 PID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3679663/