We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

shell output problem

limneos
limneos asked
on
Medium Priority
343 Views
Last Modified: 2008-01-16
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.
Comment
Watch Question

Top Expert 2005
Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Commented:
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.
Commented:
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);
    }
?>
CERTIFIED EXPERT
Commented:
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.

Author

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
Commented:
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

Author

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?

Author

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?

Author

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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.