简体   繁体   中英

Grab results from a php exec() while the command is still running?

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.

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

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";

?>

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

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM