2016-11-21 62 views
4

在PHP后台进程我在Windows Apache服务器后台启动的进程。执行和获得的pid在Windows

的index.php以下这一点:

<?php 
$cmd = "C:/xampp/php/php.exe -f test.php"; 
pclose(popen("start /B ". $cmd, "r")); 
echo "OK"; 
?> 

test.php的以下这一点:

<?php 
sleep(5); 
file_put_contents("1.txt", date("Y-m-d H:i:s")); 
?> 

在那个时候,我想其中的pid php -f test.php。 当我开始的index.php,我可以在tasklist命令行的输出看到新php.exe过程。 我该如何获得这个后台进程的pid。

谢谢。

+0

'$ PID = getmypid();'http://php.net/manual/en/function.getmypid.php听从警告虽然:**警告进程ID不是唯一的,因此它们是一个弱熵源。我们建议不要依赖安全相关背景下的pid。** – icecub

+0

感谢您的帮助。但它返回httpd.exe的pid。我想获得php.exe的pid。 –

+0

我不知道你到底在做什么,但请记住这是一个CLI命令。这意味着如果你在浏览器中运行它将无法工作。 – icecub

回答

2

这将输出任务后ProcessID一直使用wmic执行。然后,您可以将其存储在会话或Cookie中以在页面之间传递。

$cmd = 'wmic process call create "C:/xampp/php/php.exe -f /path/to/htdocs/test.php" | find "ProcessId"'; 

$handle = popen("start /B ". $cmd, "r"); 

$read = fread($handle, 200); //Read the output 

echo $read; //Store the info 

pclose($handle); //Close 

输出

ProcessId = 1000 
+1

它工作正常,谢谢。 –