views:

62

answers:

3

As an extension to question "php execute a background process":

Suppose I wanted to keep a process running during a PHP session, like an interactive bash shell. How can I establish redirection of stdout/stdin such that PHP can read/write to the process?

UPDATE: The key is to be able to kick off the process and keep it running in the background so that later requests can access its stdout/stdin streams.

A: 

I would use PHP Expect. The documentation has some very good usage examples.

psmears
Once the process is started, how do you get its handle back on successive requests?
spoulson
@spoulson: Ah, I see - your question is clearer now you've updated it :-). That's much harder, let me think...
psmears
A: 

If you're using PHP to relay user-typed commands to a shell, you can use mulitple calls to shell_exec

shell_exec will return the complete output which you can then echo back to the user.

webbiedave
A: 

If you're using Linux, you can access the proc file system at /proc. Though distributions may differ somewhat, in Ubuntu Server I can find my stdio at /proc//fd/[012]. 0 is stdin, 1 is stdout, and 2 is stderr. This will probably not work if you are redirecting these from or to /dev/null, as some methods of spinning off long running background processes have you do. If you have access to the pid of the background process (a la http://nsaunders.wordpress.com/2007/01/12/running-a-background-process-in-php/), you should be able to access the stdin / stdout of the process in Linux.

michaelc