How to show status of a running program

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View


I have a command line program I run from a php script with exec. It
outputs its status as it runs to stderr. I wonder if there's a PHP way
to show the user the status of the running app? Maybe there's a timer
I could run sever side and refresh the page at various time intervals
so the user could see?

I'm not asking for the code but if you cite the "building blocks" I'd
need it would help, or any links to do what I want to do. Thanks.

Lee G.

Re: How to show status of a running program

goldtech wrote:
Quoted text here. Click to load it

That's tough, Lee.  Output isn't going to a script running under the PHP
process (web server or CLI); rather it is going to some CLI process.
This isn't just a PHP problem - it's one shared by all languages.

You *could* possibly redirect stderr to a file and parse that file, but
it's open to a LOT of "gotchas".  Better would be for the CLI to share
it's status via some interprocess communications method.  You could use
shared memory, a TCP/IP port or other methods.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: How to show status of a running program

El 12/08/2010 1:40, goldtech escribió/wrote:
Quoted text here. Click to load it

If it can be done with PHP (I don't really know) you probably need to
use the proc_... function family:

# proc_close — Close a process opened by proc_open and return the exit
code of that process
# proc_get_status — Get information about a process opened by proc_open
# proc_nice — Change the priority of the current process
# proc_open — Execute a command and open file pointers for input/output
# proc_terminate — Kills a process opened by proc_open

-- - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web:
-- Mi web de humor satinado:

Re: How to show status of a running program

Quoted text here. Click to load it

I'm not sure what kind of "status" you're looking for.  There's "I'm
still running" and "I'm done, here's my exit status." Or, there's "I'm
running and this is what I'm doing right now."

Quoted text here. Click to load it

My suggestion would be to use a two-step approach:

1. The initial page script launches the process in the background,
redirecting STDERR to a logfile:

/path/to/program & 2>/var/log/program.log

So, it launches the script and promptly forgets about it.

2. A separate script would read and output the program.log back to the
browser.  You could call this with an AJAX routine that runs every 2-3
seconds after the page loads, for example.

Granted my suggestion assumes that everything the script outputs is
what you want to display to your users.  If that's not the case, then
Jerry's suggestion holds:  you'll want some sort of shared memory or
socket connector to be able to "ask" the program how it's doing.

Either way, if this is not a CLI application, you don't want a process
that launches your program and waits for it to complete.  Even with
sockets, I would still suggest the two step approach:

1. Script 1 backgrounds the program and exits
2. Script 2 opens socket to program, gets status update and exits

Site Timeline