Nizzy
Nizzy

Reputation: 1889

php run in the background and exit when done

$escaped_check = escapeshellcmd("/usr/bin/php -f /opt/status/check.php " . $_SERVER['REMOTE_ADDR'] . " >> /dev/null 2>&1 &"); shell_exec($escaped_check);

I am trying to execute the above code in the background for the sake of non-blocking thread, but I am not sure how to exit the check.php when the job is done.

Upvotes: 0

Views: 467

Answers (2)

chf
chf

Reputation: 51

The PHP script (your check.php) should be terminated automatically after it finishes running at background. You probably can try using exit() or die() at the end of the PHP script (your check.php) to make sure the script does jump out .

You can also use the following command to get the $pid. For example, 23456. The "echo $!" part sends the process id.

$pid = shell_exec('php /path/to/script/cli_test.php argument1 > /dev/null 2>&1 & echo $!') 

And then in the terminal, use ps command to check if it is still running at background.

ps 23456

Upvotes: 1

rkosegi
rkosegi

Reputation: 14678

you can popen instead of shell_exec.

Then you can terminate it by proc_terminate

Or you can obtain process id by calling proc_get_status.

With PID you can terminate process also.

I hope this helps.

Upvotes: 1

Related Questions