Search code examples
phplaravelbashshelllaravel-artisan

Laravel command exec output


I am trying to make artisan command which would execute a non-ending script (more precisely it is a gulp watch command). Is it possible to output something to console in real time and not waiting for script to end?

exec($basedir . "test.sh", $output);

foreach ($output as $item) {
    $this->info($item);
}

test.sh looks like this (shortened for brevity):

echo "something"
echo "something else"

gulp watch

Since command doesn't actually end, I can't retrieve two echos running inside it. Is there a way to do it?


Solution

  • I have resolved it by doing the following:

    while (@ ob_end_flush()) ; // end all output buffers if any
    
    // this section streams output to terminal 
    $proc = popen($basedir . "test.sh", 'r');
    while (!feof($proc)) {
        $this->info(fread($proc, 4096));
        @ flush();
    }