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.
LVL 1
limneosAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
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);
    }
?>
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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.
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
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
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?
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?
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.