2

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?

hakre
  • 193,403
  • 52
  • 435
  • 836
  • There are at least two places where data can get stuck in buffering -- PHP to client and command to PHP. I'm sure a few minutes with the PHP manual can show the correct answer for the PHP to client portion but you may also need to do something for the command to PHP portion. Which commands specifically are you trying to use? – sarnold Jun 10 '12 at 01:13

1 Answers1

1

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.

Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87