Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke external shell script from PHP and get its process ID

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?

like image 799
Christian Studer Avatar asked Sep 24 '09 10:09

Christian Studer


People also ask

How do I find my shell process ID?

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"

How do I run a shell script in PHP?

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.

What is process ID in shell?

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.

How do I run a PHP script in Linux?

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() ..


1 Answers

$command =  'yourcommand' . ' > /dev/null 2>&1 & echo $!; ';  $pid = exec($command, $output);  var_dump($pid); 
like image 77
Boris Guéry Avatar answered Sep 23 '22 05:09

Boris Guéry