我首先执行以下命令,
$retVal = proc_close(proc_open("rfcomm connect \/dev\/rfcomm0 &> /dev/null 2> /dev/null &", array(), $pipe));
我必须在proc_open之后立即使用proc_close或命令挂起我的php页面。稍后我的脚本如果我试图找出这个后台进程的PID,我只会返回我的grep请求:
$val = shell_exec("ps aux | grep rfcomm0");
它只返回我的grep命令的进程:
www-data 6412 0.0 0.3 2004 624? S 12:23 0:00 grep rfcomm0
现在如果我从shell_exec输入完全相同的命令到我服务器上的SSH终端,我得到以下内容:
www-data 6047 0.0 0.3 1752 592 ? S 12:10 0:00 rfcomm connect /dev/rfcomm0
root 6415 0.0 0.4 3532 804 pts/1 S+ 12:27 0:00 grep rfcomm0
第一个结果是我想要的PID。 PHP无法检索自己进程的PID,还是有另一种方法来解决这个谜团?
谢谢!
答案 0 :(得分:2)
您可以使用proc_get_status ...
获取PIDhttp://php.net/manual/en/function.proc-get-status.php
此函数返回一个数组,其中一个元素是pid。它的输入参数是proc_open的返回...
答案 1 :(得分:0)
通过使用proc_get_status()
,您可以从proc_open
值中获取pid
进程ID,并获取打开的命令进程ID,这是父PID的下一个数字(父PID + 1 ):
$desc = [
["pipe","r"],
["pipe","w"],
["pipe","w"],
];
$p = proc_open('php ./process.php', $desc, $pipes);
$s = proc_get_status($p);
$parentPID = $s['pid']; // proc_open itself
$processPID = $s['pid'] + 1; // 'php ./process.php'
proc_close($p);