I’ve been using this function to check if a process is running or not. (I got it from http://nsaunders.wordpress.com/2007/01/12/running-a-background-process-in-php/):
function Is_Process_Running($PID){
exec("ps $PID", $ProcessState);
return(count($ProcessState) >= 2);
}
My understanding of it is that the function will only return if the array $ProcessState has more than two lines in it, which it will if the process is active.
So it’s thus easy to code something to perform an action whilst the process is running as below (also from above link):
while(is_process_running($PID))
{
echo(" . ");
ob_flush(); flush();
sleep(1);
}
But then what’s the best way to do perform an action when the process has completed? I tried:
while(! is_process_running($PID))
but this doesn’t work, I think because it only goes through the loop once. Any help is much appreciated, thanks!
Just put the code to be performed after the loop:
If you need to branch depending on whether the process is running at all in the first place, you can wrap the entire thing in a
if (is_process_running($PID))conditional.