Search code examples
phppid

Find PID of created PHP process in PHP


I first execute the following command,

 $retVal = proc_close(proc_open("rfcomm connect \/dev\/rfcomm0 &> /dev/null 2> /dev/null &", array(), $pipe));

I have to use proc_close right after proc_open or the command hangs my php page. Later on my script if I try to find out the PID of this background process I only get returned my grep request:

$val = shell_exec("ps aux | grep rfcomm0");

It only returns the process of my grep command:

www-data 6412 0.0 0.3 2004 624 ? S 12:23 0:00 grep rfcomm0

Now if I enter the exact same command from shell_exec into my SSH terminal on my server I get the following:

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

The first result is the PID I want. Is PHP unable to retrieve the PID of its own processes or is there another way to solve this mystery?

Thanks!


Solution

  • You can get PID using proc_get_status ...

    http://php.net/manual/en/function.proc-get-status.php

    This function returns an array which one of it's element's are pid. It's input parameter is return of proc_open ...