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?
To get the PID of the last executed command type: echo "$!" Store the pid of the last command in a variable named foo: foo=$! Print it, run: echo "$foo"
The shell_exec() function is an inbuilt function in PHP which is used to execute the commands via shell and return the complete output as a string. The shell_exec is an alias for the backtick operator, for those used to *nix.
Parent and Child Processes Each unix process has two ID numbers assigned to it: The Process ID (pid) and the Parent process ID (ppid). Each user process in the system has a parent process. Most of the commands that you run have the shell as their parent.
You can execute linux commands within a php script - all you have to do is put the command line in brackits (`). And also concentrate on exec() , this and shell_exec() ..
$command = 'yourcommand' . ' > /dev/null 2>&1 & echo $!; '; $pid = exec($command, $output); var_dump($pid);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With