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
2009-09-24 12:13:14
+5
A:
$command = 'yourcommand' . ' > /dev/null 2>&1 & echo $!; ';
$pid = exec ( $command, $output );
var_dump($pid);
Boris Guéry
2009-09-24 12:17:29
Hehehe, that's actually creative, thank you.
christian studer
2009-09-25 07:59:30
Much better than my answer!
andho
2009-09-25 08:47:47
What's the meaning of " > /dev/null 2> "
andho
2009-09-25 08:48:21
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
2009-09-25 15:33:07