• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

shell output problem

Hello.

I'm trying to run a command using e.g. system('ls -l') and have the output printed while it's being processed , not gathered in total when it's finished.

I tried:

ob_start();
system('ping -c4 www.someip.com');
$output = ob_get_contents();
ob_end_clean();
print "<pre>";
echo $output;
print "</pre>";


The above example works, but it prints the result when it's finished,
which means that if I want to execute a long script and watch the progress on the the web page,
I will have to wait for the process to finish and then view it in total.

Is there a way to view the progress second by second, or in real time, printed on the web page?

Thank you in advance for your help.
0
limneos
Asked:
limneos
  • 4
  • 2
  • 2
  • +1
5 Solutions
 
snoyes_jwCommented:
removing the output buffering would help.
but it's not an easy thing to do on a web page.  The web server has its own buffer that you must overcome.
0
 
TomeeboyCommented:
So, you're wanting to view the results that are displayed, line by line in real time?  for a single command (such as ping)?  I don't think it works like that... but maybe there's a method I don't know of.

Now, if you're wanting to do multiple commands within the same script and want the output from each one displayed as they are completed, that can be done.
0
 
TeRReFCommented:
I think this is the only way:

<?php
    for ($i=0; $i < 10; $i++) {
        $output = system('ping -c1 www.google.com');
        print "<pre>";
        echo $output;
        print "</pre>";
        ob_flush();
        flush();
        sleep(1);
    }
?>
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
TomeeboyCommented:
Yould could try adding:

ob_end_flush();
ob_flush();
flush();

At the end of your code.. if there's anything left in an output buffer, that should get it out.
0
 
limneosAuthor Commented:
Well, thank you but none of the above work as expected.

The example of TeRReF simply prints out the same thing 10 times.

In order to be more specific and help you all understand what I'm trying to achieve, I'll give you a particular example that most of us have seen.

When we use Cpanel(TM) , and let's say, we do an apache update, it takes us to a page where we can see the progress of the update, just like it's printed on shell.It is not fake, it gets values right from the shell.

It reads the lines from shell either in real time, or by making the php script to "wait" for a while until the results are updated...

Something like this...Thank you all for your assistance
0
 
TeRReFCommented:
It does not print the same thing, it gives you 10 different pings. You will need processes to finish in order to do the:
        ob_flush();
        flush();
That is the only way in PHP to dump data to screen without refreshing the page.

If you want real time updates from shell processes, you could use something like AJAX. That means incorporating Javascript to cooperate with your PHP code.
http://en.wikipedia.org/wiki/Ajax_%28programming%29
0
 
limneosAuthor Commented:
could we possible ask from the process to run on the background and echo its output to a file and read the file continuously?
0
 
limneosAuthor Commented:
and one more hint I need:
Is there any way to display the error messages that exec or system returns?
e.g. system('aiuluad -ls);

-bash: aiuluad: command not found

so we can see what the error was?
0
 
limneosAuthor Commented:
Well....the last problem I had, I solved it from the manual ;)

$command="ls /root";
system( $command . " &> /tmp/error " );
$filename = "/tmp/error";
$fd = fopen ($filename, "r");
$contents = fread ($fd, filesize ($filename));
fclose ($fd);
print "<pre>$contents</pre>";

Prints  : ls: /root: Permission denied

Still looking for a solution of the first problem....

Thank you all
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now