1
votes

I am having a hard time identifying the solution to my problem. Here is a simple explanation.

Using the following code, I am able to launch a script using the shell_exec() function :

shell_exec("nohup /usr/bin/php /home/script.php > /dev/null & echo $!");

This works perfectly.

But when I launch the PHP script using the shell_exec() function and assigning the returned PID using the following code, the script process is not launched and no action is completed (although the $pid is echoed correctly) :

$pid = shell_exec("nohup /usr/bin/php /home/script.php > /dev/null & echo $!");
echo 'Process ID : ' . $pid;

How can I retrieve the PID and execute the process for this "script.php" ?

1
Have you tried running nohup /usr/bin/php /home/script.php > /dev/null & echo $! directly on the command line to observe the results? shell_exec returns whatever the output of the program run is. - Devin H.
echo $! will return the process PID and this is what I need. Running the shell_exec() function works if no variable is assigned from the return. Is there a way to echo PID from "script.php" ? (Answer to your question : I have no direct access to shell for the moment.) - wlarcheveque

1 Answers

0
votes

I finally found the problem with the process :

Fact - PID was return before the script was executed

Fact - "isPIDRunning()" Verification is made in "script.php" before it is executed.

Result - Instead of avoiding the script to be launched twice, it is not even executed.