views:

1184

answers:

5

Hi,

I am trying to run a process on a web page that will return its output in realtime. For example if I run 'ping' process it should update my page every time it returns a new line (right now, when I use exec(command, output) I am forced to use -c option and wait until process finishes to see the output on my web page). Is it possible to do this in php?

I am also wondering what is a correct way to kill this kind of process when someone is leaving the page. In case of 'ping' process I am still able to see the process running in the system monitor (what makes sense).

+1  A: 

See this:

Is there a way to have PHP print the data to a web browser in real time?

karim79
Thanks for your reply, but what your code does in my browser is wait for 15 seconds and then print out "Hi my name isED" string. I mean page is not reloading each time after ob_flush call as I expected.I am sorry, I am new to php and web development, but I can't think of how I can apply ob_start/ob_flush to my problem with running a process.
negative
@nigative - see my edit, this question has been asked before, I put in the link to it.
karim79
Thanks, sorry for a stupid question but how it can help me to combine it with exec function while a process is still running? What I see so far, exec writes to an output variable only when the process is finished.
negative
A: 

This should also work just fine, no need for ob_start():

for ($i = 1; $i <= 5; $i++)
{
    echo $i . ' ';
    sleep($i);
    flush();
}
Alix Axel
again, same problem for me, it waits for 15 seconds and then prints out 1 2 3 4 5.And my question is mostly about grabbing and printing real-time output from a process rather than just reloading a page.I know my tech communication skills suck as well as my knowledge of php, sorry.
negative
In comment to karim79's answer you said "I mean page is not reloading each time after ob_flush call as I expected" but now your saying you don't want to reload the page.
andho
+1  A: 

If you're looking to run system commands via PHP look into, the exec documentation.

I wouldn't recommend doing this on a high traffic site though, forking a process for each request is quite a hefty process. Some programs provide the option of writing their process id to a file such that you could check for, and terminate the process at will, but for commands like ping, I'm not sure that's possible, check the man pages.

You may be better served by simply opening a socket on the port you expect to be listening (IE: port 80 for HTTP) on the remote host, that way you know everything is going well in userland, as well as on the network.

If you're attempting to output binary data look into php's header function, and ensure you set the proper content-type, and content-disposition. Review the documentation, for more information on using/disabling the output buffer.

Nathacof
A: 

First check whether flush() works for you. If it does, good, if it doesn't it probably means the web server is buffering for some reason, for example mod_gzip is enabled.

For something like ping, the easiest technique is to loop within PHP, running "ping -c 1" multiple times, and calling flush() after each output. Assuming PHP is configured to abort when the HTTP connection is closed by the user (which is usually the default, or you can call ignore_user_abort(false) to make sure), then you don't need to worry about run-away ping processes either.

If it's really necessary that you only run the child process once and display its output continuously, that may be more difficult -- you'd probably have to run it in the background, redirect output to a stream, and then have PHP echo that stream back to the user, interspersed with regular flush() calls.

Todd Owen
A: 

Try changing the php.ini file set "output_buffering = Off". You should be able to get the real time output on the page Use system command instead of exec.. system command will flush the output