Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

exec

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.

like image 692
Kyle Avatar asked Dec 12 '22 08:12

Kyle


1 Answers

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

like image 117
Sietse Avatar answered Apr 12 '23 22:04

Sietse