shell output problem


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:

system('ping -c4');
$output = ob_get_contents();
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.
Who is Participating?

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

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.

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
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.
I think this is the only way:

    for ($i=0; $i < 10; $i++) {
        $output = system('ping -c1');
        print "<pre>";
        echo $output;
        print "</pre>";
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.

Yould could try adding:


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
It does not print the same thing, it gives you 10 different pings. You will need processes to finish in order to do the:
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.
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

From novice to tech pro — start learning today.