79

I am trying to run a process on a web page that will return its output in realtime. For example if I run 'ping' process it should update my page every time it returns a new line (right now, when I use exec(command, output) I am forced to use -c option and wait until process finishes to see the output on my web page). Is it possible to do this in php?

I am also wondering what is a correct way to kill this kind of process when someone is leaving the page. In case of 'ping' process I am still able to see the process running in the system monitor (what makes sense).

2
  • 1
    Nginx X-Accel buffering can prevent the accepted answer from working, to make it work, add this header before: ``` header('X-Accel-Buffering: no'); ```
    – Nylls
    Apr 20, 2020 at 14:41
  • for me Nginx buffering was the issue above comment solved my problem. Nov 21, 2020 at 13:44

12 Answers 12

105

This worked for me:

$cmd = "ping 127.0.0.1";

$descriptorspec = array(
   0 => array("pipe", "r"),   // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),   // stdout is a pipe that the child will write to
   2 => array("pipe", "w")    // stderr is a pipe that the child will write to
);
flush();
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());
echo "<pre>";
if (is_resource($process)) {
    while ($s = fgets($pipes[1])) {
        print $s;
        flush();
    }
}
echo "</pre>";
8
  • This doesn't doing anything for me, on Fedora php 5+ Jan 2, 2012 at 21:55
  • 22
    +1 works for me, but only if I add ob_implicit_flush(true);ob_end_flush(); at the beginning
    – tetris11
    Sep 27, 2012 at 14:15
  • 2
    Combine this with Robb's answer and you have a goer.
    – Petah
    Feb 7, 2013 at 22:45
  • I wrapped this in a function so I could call it just like shell_exec. Works nicely if you just want to show the output and not do any processing on it.
    – jxmallett
    Jan 16, 2015 at 6:58
  • Good example is on proc_open() PHP manual page
    – sobi3ch
    Nov 10, 2015 at 12:06
39

This is a nice way to show real time output of your shell commands:

<?php
header("Content-type: text/plain");

// tell php to automatically flush after every output
// including lines of output produced by shell commands
disable_ob();

$command = 'rsync -avz /your/directory1 /your/directory2';
system($command);

You will need this function to prevent output buffering:

function disable_ob() {
    // Turn off output buffering
    ini_set('output_buffering', 'off');
    // Turn off PHP output compression
    ini_set('zlib.output_compression', false);
    // Implicitly flush the buffer(s)
    ini_set('implicit_flush', true);
    ob_implicit_flush(true);
    // Clear, and turn off output buffering
    while (ob_get_level() > 0) {
        // Get the curent level
        $level = ob_get_level();
        // End the buffering
        ob_end_clean();
        // If the current level has not changed, abort
        if (ob_get_level() == $level) break;
    }
    // Disable apache output buffering/compression
    if (function_exists('apache_setenv')) {
        apache_setenv('no-gzip', '1');
        apache_setenv('dont-vary', '1');
    }
}

It doesn't work on every server I have tried it on though, I wish I could offer advice on what to look for in your php configuration to determine whether or not you should pull your hair out trying to get this type of behavior to work on your server! Anyone else know?

Here's a dummy example in plain PHP:

<?php
header("Content-type: text/plain");

disable_ob();

for($i=0;$i<10;$i++) 
{
    echo $i . "\n";
    usleep(300000);
}

I hope this helps others who have googled their way here.

7
  • 1
    This doesn't work with exec(). system() works fine though. May 29, 2013 at 15:15
  • This worked really well for me, It doesn't update constantly but every so often. Its better then nothing. Thank you.
    – Exoon
    Dec 20, 2013 at 21:58
  • this produced the desired effect for me on CentOS 6.5 / apache 2.2 / php 5.3. cheers!
    – mxmader
    Apr 8, 2014 at 16:53
  • This works great for me (OS X 10.12, Apache 2.2.3, PHP 7.0.9), but for some reason only when the X-Content-Type-Options header is also set: header('X-Content-Type-Options: nosniff'); Nov 16, 2016 at 16:13
  • Since the browsers usually wait for some initial pack of data, you can send a lot of whitespace at the beginning, so that the initial buffer is filled: for ($i=0; $i < 100000; $i++) { echo " "; } echo "\n"; flush(); Aug 11, 2017 at 9:24
13

Checked all answers, nothing works...

Found solution Here

It works on windows (i think this answer is helpful for users searching over there)

<?php
    $a = popen('ping www.google.com', 'r'); 
    
    while($b = fgets($a, 2048)) { 
        echo $b."<br>\n"; 
        ob_flush();flush(); 
    }

    pclose($a); 
?>
1
  • 1
    This is the one that worked for me when I had to run a python script. Others didn't work as well.
    – J. Martin
    Nov 1, 2020 at 6:36
7

A better solution to this old problem using modern HTML5 Server Side Events is described here:

http://www.w3schools.com/html/html5_serversentevents.asp


Example:

http://sink.agiletoolkit.org/realtime/console

Code: https://github.com/atk4/sink/blob/master/admin/page/realtime/console.php#L40

(Implemented as a module in Agile Toolkit framework)

1
  • IE/Edge does not support this... ;)
    – ed22
    Apr 27, 2016 at 14:43
6

For command-line usage:

function execute($cmd) {
    $proc = proc_open($cmd, [['pipe','r'],['pipe','w'],['pipe','w']], $pipes);
    while(($line = fgets($pipes[1])) !== false) {
        fwrite(STDOUT,$line);
    }
    while(($line = fgets($pipes[2])) !== false) {
        fwrite(STDERR,$line);
    }
    fclose($pipes[0]);
    fclose($pipes[1]);
    fclose($pipes[2]);
    return proc_close($proc);
}

If you're trying to run a file, you may need to give it execute permissions first:

chmod('/path/to/script',0755);
0
5

try this (tested on Windows machine + wamp server)

        header('Content-Encoding: none;');

        set_time_limit(0);

        $handle = popen("<<< Your Shell Command >>>", "r");

        if (ob_get_level() == 0) 
            ob_start();

        while(!feof($handle)) {

            $buffer = fgets($handle);
            $buffer = trim(htmlspecialchars($buffer));

            echo $buffer . "<br />";
            echo str_pad('', 4096);    

            ob_flush();
            flush();
            sleep(1);
        }

        pclose($handle);
        ob_end_flush();
2
  • 2
    This answer is wrong. The question was how to get page output in realtime. With ob_start() you do just the opposite: You write all output to an internal server buffer instead of sending it immediately to the browser!
    – Elmue
    Nov 26, 2015 at 13:13
  • 1
    i have tried ur code,,actually its not displaying shell command in real time, but just wait shell command to finish load all text and displaying it bit by bit using sleep(1) help. sorry, u deserve downvote
    – Ryan Arief
    Jun 28, 2016 at 8:14
5

I've tried various PHP execution commands on Windows and found that they differ quite a lot.

  • Don't work for streaming: shell_exec, exec, passthru
  • Kind of works: proc_open, popen -- "kind of" because you cannot pass arguments to your command (i.e. wont' work with my.exe --something, will work with _my_something.bat).

The best (easiest) approach is:

  1. You must make sure your exe is flushing commands (see printf flushing problem). Without this you will most likely receive batches of about 4096 bytes of text whatever you do.
  2. If you can, use header('Content-Type: text/event-stream'); (instead of header('Content-Type: text/plain; charset=...');). This will not work in all browsers/clients though! Streaming will work without this, but at least first lines will be buffered by the browser.
  3. You also might want to disable cache header('Cache-Control: no-cache');.
  4. Turn off output buffering (either in php.ini or with ini_set('output_buffering', 'off');). This might also have to be done in Apache/Nginx/whatever server you use in front.
  5. Turn of compression (either in php.ini or with ini_set('zlib.output_compression', false);). This might also have to be done in Apache/Nginx/whatever server you use in front.

So in your C++ program you do something like (again, for other solutions see printf flushing problem):

Logger::log(...) {
  printf (text);
  fflush(stdout);
}

In PHP you do something like:

function setupStreaming() {
    // Turn off output buffering
    ini_set('output_buffering', 'off');
    // Turn off PHP output compression
    ini_set('zlib.output_compression', false);
    // Disable Apache output buffering/compression
    if (function_exists('apache_setenv')) {
        apache_setenv('no-gzip', '1');
        apache_setenv('dont-vary', '1');
    }
}
function runStreamingCommand($cmd){
    echo "\nrunning $cmd\n";
    system($cmd);
}
...

setupStreaming();
runStreamingCommand($cmd);
3

First check whether flush() works for you. If it does, good, if it doesn't it probably means the web server is buffering for some reason, for example mod_gzip is enabled.

For something like ping, the easiest technique is to loop within PHP, running "ping -c 1" multiple times, and calling flush() after each output. Assuming PHP is configured to abort when the HTTP connection is closed by the user (which is usually the default, or you can call ignore_user_abort(false) to make sure), then you don't need to worry about run-away ping processes either.

If it's really necessary that you only run the child process once and display its output continuously, that may be more difficult -- you'd probably have to run it in the background, redirect output to a stream, and then have PHP echo that stream back to the user, interspersed with regular flush() calls.

3

If you're looking to run system commands via PHP look into, the exec documentation.

I wouldn't recommend doing this on a high traffic site though, forking a process for each request is quite a hefty process. Some programs provide the option of writing their process id to a file such that you could check for, and terminate the process at will, but for commands like ping, I'm not sure that's possible, check the man pages.

You may be better served by simply opening a socket on the port you expect to be listening (IE: port 80 for HTTP) on the remote host, that way you know everything is going well in userland, as well as on the network.

If you're attempting to output binary data look into php's header function, and ensure you set the proper content-type, and content-disposition. Review the documentation, for more information on using/disabling the output buffer.

1

Try changing the php.ini file set "output_buffering = Off". You should be able to get the real time output on the page Use system command instead of exec.. system command will flush the output

1

I had the same problem only could do it using Symfony Process Components ( https://symfony.com/doc/current/components/process.html )

Quick example:

<?php 

use Symfony\Component\Process\Process;

$process = new Process(['ls', '-lsa']);
$process->run(function ($type, $buffer) {
    if (Process::ERR === $type) {
        echo 'ERR > '.$buffer;
    } else {
        echo 'OUT > '.$buffer;
    }
});

?>
1
  • This makes things so easy. 10/10 Mar 24, 2023 at 10:39
0

why not just pipe the output into a log file and then use that file to return content to the client. not quite real time but perhaps good enough?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.