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.

Community
  • 1
  • 1
spoulson
  • 21,335
  • 15
  • 77
  • 102

3 Answers3

2

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

psmears
  • 26,070
  • 4
  • 40
  • 48
  • 1
    Once the process is started, how do you get its handle back on successive requests? – spoulson Jul 14 '10 at 20:24
  • @spoulson: Ah, I see - your question is clearer now you've updated it :-). That's much harder, let me think... – psmears Jul 14 '10 at 21:12
1

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/<pid>/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.

Iiridayn
  • 1,747
  • 21
  • 43
0

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
  • 48,414
  • 8
  • 88
  • 101