Tutorial :How can I capture the PID and output of a backgrounded PHP process at the same time?


I have one PHP script that has to start a second script in the background using exec. The parent script needs to capture the PID of this new process, but I also need to capture any potential output of the child script so it can be logged in the database. I can't just have the child script log to the database because I also need to capture fatal errors or anything that might indicate a problem with the script. The PID is used because the parent process needs to be able to check on the child and see when it finished. The child processes are sometimes run as crons, so forking isn't an option here either. I don't want two execution paths to debug if there are problems.

This was my first solution and it can capture the output, but fails to get the correct PID.

// RedirectToLog.php just reads stdin and logs it to the databse.  $cmd="php child.php </dev/null 2>&1 | php RedirectToLog.php >/dev/null 2>&1 & echo $!";  

The problem here is that $! is the PID of the last process that was started in the background which ends up being RedirectToLog.php instead of child.php.

My other idea was to attempt to use a FIFO file (pipe).

$cmd1="php RedirectToLog.php </tmp/myFIFO >/dev/null 2>&1 &"  $cmd2="php child.php </dev/null >/tmp/myFIFO 2>&1 & echo $!"  

This one didn't work because I couldn't get RedirectToLog to reliably consume the fifo and when it did, sometimes child.php failed to write EOF to the pipe which left both ends waiting on the other and both processes would hang until one was killed.


use proc_open to get full fd connectivity while starting a process. Take the resource returned by proc_open and use proc_get_status to get the pid.

$descriptors = array( ... ); // i suggest you read php.net on this  $pipes = array();  $res = proc_open($cmd,$descriptors,$pipes);    $info = proc_get_status($res);  echo $info['pid'];  

I haven't fully grasped all your problems, but this should get you started.


I think you need to use
proc_open &


I'm guessing the only reason you really want to capture the childs PID is to monitor it. If so, it might be easier to use proc_open. That way you can basically open file handles for stdin, stdout and stderr and monitor everything from the parent.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »