For context: I am spawning a Java application from PHP, using xvfb-run
. Now, this process creates a parent PID for the xvfb-run
and couple more children PIDs corresponding to Xvfb
and java
process.
Using the following command, I have managed to get the parent PID (of xvfb-run
):
$cmd1 = 'nohup xvfb-run -a java -jar some.jar > /dev/null 2>&1 & echo $!';
$res1 = trim(shell_exec($cmd1));
var_dump($res1); // Returns the parent PID. For eg: 26266
Now, once the Java process is complete, I will need to kill the Java process, to free up my server resources. (Java application is GUI based, and I have managed to get it working without utilising GUI control, but still I can close it, only by using kill -9 <pid>
Now, from the terminal, I can get the Children pid using:
pgrep -P 26266
and, it will return me pids as follows:
26624
26633
However, when I try to do the same using PHP, I cannot get these pid(s) out. I have tried exec
, shell_exec
, system
etc. My attempted script is:
$cmd2 = 'pgrep -P ' . $res1;
var_dump($cmd2);
$res2 = trim(exec($cmd2, $o2, $r2));
var_dump($res2);
var_dump($o2);
var_dump($r2);
It prints out the following:
string(14) "pgrep -P 26266" string(0) "" array(0) { } int(1)
What am I missing here ? Any pointer would be helpful. Thanks in Advance