[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

shell output problem

Posted on 2006-03-31
10
Medium Priority
?
306 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.
0
Comment
Question by:limneos
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 33

Accepted Solution

by:
snoyes_jw earned 400 total points
ID: 16344631
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
 
LVL 15

Assisted Solution

by:Tomeeboy
Tomeeboy earned 800 total points
ID: 16344699
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
 
LVL 29

Assisted Solution

by:TeRReF
TeRReF earned 800 total points
ID: 16344733
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Assisted Solution

by:Tomeeboy
Tomeeboy earned 800 total points
ID: 16344759
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
 
LVL 1

Author Comment

by:limneos
ID: 16345026
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
 
LVL 29

Assisted Solution

by:TeRReF
TeRReF earned 800 total points
ID: 16345356
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
 
LVL 1

Author Comment

by:limneos
ID: 16347021
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
 
LVL 1

Author Comment

by:limneos
ID: 16347040
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
 
LVL 1

Author Comment

by:limneos
ID: 16354042
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

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month18 days, 11 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question