简体   繁体   中英

Show output taken from shell_exec and display it in real time instead of after waiting 5-7min

Right now, I have code as follows.

$output = shell_exec( !-- unix commands are here --! );
echo $output; 

I have a website where, upon the clicking of a particular button, the shell script is outputted and it is displayed on the browser. This is working perfectly. The only issue is that I can't see what's happening with the output until it is finished. I have to wait about 5-7 minutes, and then I see about a hundred lines of output. I am trying to push the output to the browser as the output executes -- I want to be able to see the output as its happening in real time (on the browser).

I've tried to use popen , proc_open , flush() , ob_start , etc. Nothing seems to be working. I just tried opening a text file, writing the contents of the output to the textfile, and reading the textfile incrementally on a loop. I'm a php beginner so it's possible that I haven't been using any of the above methods properly.

What is the simplest way to accomplish this?

Because PHP runs exec, system, pass_thru, etc in blocking mode, you are very limited in possibilities. PHP will require the code to finish executing before moving on throughout the script, unless you do something like add the following to your command:

"> /dev/null 2>/dev/null &"

Of course, this will halt the output of your command, but.. maybe something like:

exec('command > /cmd_file 2>/cmd_file &');

$file = fopen('/cmd_file', 'r');
while (!feof($file)) {
    echo fgets($file);
    sleep(1);
}

fclose($file);

Worth a shot.

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