Popup and file operations
related to this issue
my script mostly works fine, but sometimes it stops responding to the fread function call and I can't find the reason for the failure.
private function run_lengthy_job($command, $message) {
for($handle = popen($command, 'r'); !feof($handle); sleep(2)) {
printf("[%s]\t%s\n", time(), $message);
// log the operation
exec(sprintf('logger "%s"',
escapeshellarg(fread($handle, 1024))));
}
pclose($handle);
}
Example command
hg clone -v --debug http://some.repository.com/hgwebdir.cgi/some_repo some_repo
until it works, cloning a large repository. this same problem persists even if I change fread to fgets.
Summary of my php environment,
PHP 5.2.4-2ubuntu5.6 with Suhosin-Patch 0.9.6.2 (cli) (built: Apr 17 2009 14:29:38)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
with Xdebug v2.0.3, Copyright (c) 2002-2007, by Derick Rethans
Works on ubuntu 8.04.2
EDIT: Tried proc_open instead of popen and the script got stuck in one place. EDIT: replaced fread with stream_get_contents, but still stuck in one place ...
0
a source to share
1 answer
My current solution
private function run_lengthy_job($command, $message) {
printf("%s\t", $message);
for($handle = popen($command, 'r'),
stream_set_blocking($handle, FALSE),
stream_set_timeout($handle, 3);
!feof($handle); sleep(1)) {
echo '.';
exec(sprintf('logger "%s"',
escapeshellarg(stream_get_contents($handle))));
}
$exit_status = pclose($handle);
if(pcntl_wifexited($exit_status)
&& pcntl_wexitstatus($exit_status) == 0) {
echo "done!\n";
} else {
echo "failed!\n";
}
}
the long wait time that occurs when calling fread is probably due to the fact that access to the $ descriptor is blocked so that popen can write output to it.
0
a source to share