Proper shell execution in PHP Proper shell execution in PHP php php

Proper shell execution in PHP


Well, seems a year passed and forgot this thing is still pending!

However, I wrapped up this mess in a nice PHP class which you can find on Github.

The main remaining problem is that reading STDERR causes the PHP script to block, so it has been disabled.

On the bright side, thanks to events and some nice coding (I hope!), one can actually interact with the process being executed (hence the class name, InterExec). So you can have bot-style behavior in PHP.


You've missed this note in the PHP manual for stream_select():

When stream_select() returns, the arrays read, write and except are modified to indicate which stream resource(s) actually changed status.

You need to re-create the arrays before calling stream_select() each time.

Depending on the process you're opening, this may be why your example still blocks.


while($r = stream_select($read, $write, $except, null, $timeout)){

As far as I know this will set $r to the number of changed streams, which may be 0 and the loop would no longer continue. I would personally recode this as described in the PHP manual:

while(false !== ($r = stream_select($read, $write, $except, null, $timeout))){

As far as your STDIN is concerned if your process is not interactive then the STDIN may not be necessary. What is the process you are executing?