Search code examples
phplinuxshell-exec

shell_exec() function not working when echoed PID assigned as PHP variable


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" ?


Solution

  • 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.