4

When I run an exec from PHP like so:

$result = exec('command');

The results from this will be stored in $result. But in my current case, my command can take a few minutes and outputs results as it is running. Is there a way I can get output while it is running? I know that the passthru method will output the results to be browser, but I actually want it directly.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
Kyle
  • 17,317
  • 32
  • 140
  • 246

5 Answers5

6

You should take a look at proc_open

After making the output stream non-blocking (with stream_set_blocking), you can read from it whenever you want without having your PHP-code blocked.

-Edit- If you use

$result = exec('command > /path/to/file &');

It will run in the background and you can read the output in /path/to/file

LF00
  • 27,015
  • 29
  • 156
  • 295
Sietse
  • 707
  • 4
  • 10
5

Maybe not the best way to do it (but worked for me):

<?php

$cmd = "ping 127.0.0.1 -c 5"; //example command

$descriptorspec = array(
    0 => array("pipe", "r"), 
    1 => array("pipe", "w"), 
    2 => array("pipe", "a")
);

$pipes = array();

$process = proc_open($cmd, $descriptorspec, $pipes, null, null);

echo "Start process:\n";

$str = "";

if(is_resource($process)) {
    do {
        $curStr = fgets($pipes[1]);  //will wait for a end of line
        echo $curStr;
        $str .= $curStr;

        $arr = proc_get_status($process);

    }while($arr['running']);
}else{
    echo "Unable to start process\n";
}

fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);

echo "\n\n\nDone\n";

echo "Result is:\n----\n".$str."\n----\n";

?>
Eddie
  • 51
  • 1
  • 1
2

specify second argument

exec('command', $result);

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().

genesis
  • 50,477
  • 20
  • 96
  • 125
  • Thanks but from my understanding this still requires the exec to finish completely before I can get the output. I'm specifically looking to get the output while the command is running so that I can update a progress bar. – Kyle Oct 09 '11 at 11:56
  • @Zenox: ah. in that case, it's not possible. PHP is not async – genesis Oct 09 '11 at 11:57
0

For whomever it may help, I've used Eddie's answer and modified it for my purposes (outputting a MySQL dump file without flooding the server's RAM)

$dumpCommand = "mysqldump --skip-lock-tables -u $dbuser -p$dbpasswd $dbname";
$dumpFileName = 'backup_'.$dbname.'-'.date('Ymd-Hi').'.sql';

$descriptorSpec = array(
    0 => array("pipe", "r"), 
    1 => array("pipe", "w"), 
    2 => array("pipe", "a")
);

$pipes = array();

$process = proc_open($dumpCommand, $descriptorSpec, $pipes, null, null);

if(!is_resource($process)) {
    die('Unable to start process');
}

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$dumpFileName.'"');

do {
    echo fgets($pipes[1]); // Will wait for EOL
    $arrStatus = proc_get_status($process);
} while($arrStatus['running']);

fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
rkok
  • 1,047
  • 14
  • 17
0

It might be possible to achieve what you need using passthru() combined with output buffering. Not sure, though.

EdoDodo
  • 8,220
  • 3
  • 24
  • 30