1

I've seen PHP reading shell_exec live output and PHP: Outputting the system / Shell_exec command output in web browser, but can't get the following working.

NB: originally, my shell script was running some python, but I have simplified it.

live.sh

uname -a
date
watch --no-title date

live.php

<?php
$cmd = "sh live.sh";

while (@ ob_end_flush()); // end all output buffers if any

$proc = popen($cmd, 'r');
echo '<pre>';
while (!feof($proc))
{
    echo fread($proc, 4096);
    @ flush();
}
echo '</pre>';
?>

The uname and date outputs appear in the browser okay, but the watch output does not.

Am I in fact attempting the impossible?

3
  • If you really need the output of a server process to replace the previously rendered text, I think you will need Ajax. Commented Aug 17, 2014 at 22:18
  • 2
    When capturing output from a python process, you'll need to disable output buffering: python -u /path/to/watch ... or PYTHONUNBUFFERED=1 watch ... Commented Aug 19, 2014 at 12:09
  • @JasperN.Brouwer You are legend, it worked for me. Commented Jan 21, 2018 at 10:05

2 Answers 2

5
+50

I would advise against the approach of using watch, and you are probably in for more trouble than you expect.

Firstly, long running command might be affected by the (default) PHP timeout, so you may have to tweak that.

Then, watch probably uses terminal sequences to clear the screen, and I am not sure how this translates to the output code.

I would rather suggest setting up a client side mechanism to periodically repeat a call to the sever side live.php.

This post on SO will help you get started. jQuery, simple polling example

the page above uses the jquery library, but you could use native Javascript equivalent if you want to.

The simplest example (from that page) would be :

function poll(){
    $("live.php", function(data){
        $("#output_container").html(data);
    }); 
}

setInterval(function(){ poll(); }, 5000);

In your page, set up a container for your results

<div id="output_container"></div>

In your example, you remove the watch from your script and replace it with the command you intended watching.

Sign up to request clarification or add additional context in comments.

Comments

1

You probably want this only for some special thing. So, for those you can try ajaxterm.

Really easy to use, (4.line installation)

wget http://antony.lesuisse.org/software/ajaxterm/files/Ajaxterm-0.10.tar.gz
tar zxvf Ajaxterm-0.10.tar.gz
cd Ajaxterm-0.10
./ajaxterm.py

and you will get full bash running interactively in the browser. (after the login/password). Written in python, so easily adaptible for you. Also, see here.

1 Comment

I might not use it for this exact issue, but very useful to know about, thank you.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.