Juan Traverso Viagas
Contributed by Juan Traverso Viagas in #8288

When running a command with the Process component, you can get back the output at the end of the process execution, or you can get it incrementally as the command is running:

1
2
3
4
5
6
7
8
9
10
11
12
use Symfony\Component\Process\Process;

$process = new Process('ls -lsa');
$process->start();

while ($process->isRunning()) {
    // get the incremental output since the last call
    echo $process->getIncrementalOutput();
}

// get the whole output at the end of the execution
echo $process->getOutput();

But when a command outputs a lot of data, and if you are using the incremental output getters, you don't need to keep around the whole output as you won't use the getOutput() method anyway. In this case, you might want to flush the output with these new methods available in 2.4:

1
2
3
4
5
6
7
while ($process->isRunning()) {
    // get the incremental output since the last call
    echo $process->getIncrementalOutput();

    // flushes the output buffer
    $process->flushOutput();
}

Everything also works for the stderr output, just use flushErrorOutput() instead.

Published in #Living on the edge