tags:

views:

102

answers:

1

related to this question

my script basically runs fine but sometimes it stop responding at the fread function call and I can't seem to find the reason of 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

for now it is failing while cloning a large repository. the same problem persists even if I change fread to fgets.

Brief information about 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

Running on ubuntu 8.04.2

EDIT: tried proc_open instead of popen and the script stuck at the same place. EDIT: replaced fread with stream_get_contents, but still stuck at the same place...

A: 

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 waiting time occurred while calling fread is probably because access to the $handle is being blocked for popen to write output into it.

Jeffrey04