I have a php script which fires off three exec() commands and gets their respective pids.
I'd like to wait for all three exec commands to stop running before moving onto the next batch of three.
I've tried checking each pi开发者_如何学Cd in a do while loop, but it never seems to come out of the loop: it just sits there until the script times out.
Here's a simplified version of what I tried:
do {
$pid = 722;
print "checking";
exec("ps -p $pid", $output);
$count = count($output);
} while ( $count >= 2 );
print "out of do while";
Does anyone have any ideas about what might cause it to never come out of the loop?
PHPs exec just keeps appending to the array, so the count will only increase. To overwrite the output array call unset($output)
before calling exec.
See the php manual on exec for more details.
If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().
From exec()
manual.
精彩评论