JakeMWP JakeMWP - 3 months ago 23
PHP Question

How to check if process is completed?

I have a php script that is currently invoked directly by a webhook. The webhook method was fine up until this past week where the volume of requests is becoming problematic for API rate limits.

What I have been trying to do is make a second PHP file to be invoked by webhooks that would check to see if there is an existing process ID for that execution file. So far I have tried to implement the answer I found here How to find out the process or script which call php bash script, and here is what I've got:

$command = "php -f $path/event-finance-reporting.php 123456789";
$execute_response = exec( "$command > /dev/null &", $arrOutput );
$check_for_running_process = exec( "ps -o pid=", $execute_response );
echo $path . "\n";
print_r( $execute_response );
print_r( $check_for_running_process );


Thanks to user l'L'l I've been able to get a further, I've updated my code to the above, and I've tried to two different commands and here are their associated results.

$command = "php -f $path/event-finance-reporting.php 123456789";


Which gives the Output:

Array(
[0] => 2261
[1] => 61632
[2] => 61634
[3] => 61635
)
61635


And:

$command = "/usr/bin/php -q -f $path/event-finance-reporting.php 123456789";


Which gives output"

Array(
[0] => 2261
[1] => 91356
[2] => 91358
[3] => 91359
)
91359

Answer

May I suggest another strategy? Have event-finance-reporting.php create a unique file in an http-accessible directory and then delete it when it finishes, e.g.,:

$f = tempnam('.../www/event-finance-reporting-processes', time());
...
// event-finance-reporting.php code
...
unlink($f);

Check the directory via http for any that are persisting for too long.

Comments