views:

451

answers:

2

How can I invoke an external shell script (Or alternatively an external PHP script) from PHP itself and get its process ID within the same script?

A: 

For most linux shell tools, it has an option to write the process ID to a file. This is how init.d scripts starts and stops apache or any other service.

For bash, in the script you can use the following to get the PID: $$
You can store it in a file then and then from php check the contents of the file to get the PID.

I am not near a linux box now but if you need more help I can test some code when i am near a linux box.

andho
+5  A: 
$command =  'yourcommand' . ' > /dev/null 2>&1 & echo $!; ';

$pid =  exec ( $command, $output );
var_dump($pid);
Boris Guéry
Hehehe, that's actually creative, thank you.
christian studer
Much better than my answer!
andho
What's the meaning of " > /dev/null 2> "
andho
it means 'send all the output to the null device, which means that all the output will be available in the stdout, and echo $! simply echo the pid, which is captured by the script
Boris Guéry