tags:

views:

151

answers:

3

I'm invoking the proc_open() and I can't capture the output of the process written to the stderr:

$curFolder = getcwd();
$procDescriptor = array( 2 => array( "pipe", "w" ) );
$cmd = "MyApp.exe -f optimization.csv";
$process = proc_open( $cmd, $procDescriptor, $pipes, $curFolder );

if( is_resource( $process ) == true ) 
{
  $procStatus = proc_get_status( $process );

  while( $procStatus['running'] === true )
  {
    if( !feof( $pipes[2] ) )
    {
      $logLine = fgets( $pipes[2] );
      echo( "Read >${logLine}<" );
    }
    sleep( 1 );
  }
}

The program hangs on the fgets(). If I run the program from the command line, everything works, i.e. there is something written to the stderr (and I also tried by using the stdout with the same result). I'm running the script on Windows - the same script on Linux runs smoothly.

A: 

From the proc_open() docs:

<?php
$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);

$cwd = '/tmp';
$env = array('some_option' => 'aeiou');

$process = proc_open('php', $descriptorspec, $pipes, $cwd, $env);

if (is_resource($process)) {
    // $pipes now looks like this:
    // 0 => writeable handle connected to child stdin
    // 1 => readable handle connected to child stdout
    // Any error output will be appended to /tmp/error-output.txt

    fwrite($pipes[0], '<?php print_r($_ENV); ?>');
    fclose($pipes[0]);

    echo stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    // It is important that you close any pipes before calling
    // proc_close in order to avoid a deadlock
    $return_value = proc_close($process);

    echo "command returned $return_value\n";
}
?>

Perhaps your process doesn't report to stderr if you find it empty

RaYell
As I wrote above, my process definitely writes something to the output and I tried both to capture the $pipes[1] and the $pipes[2] with no result.
A: 

I'd recommend also adding stdin and stdout streams, even if you don't consume the data; some C libraries get into a tizzy if the streams are not there, and can either exit early (glibc) or potentially wedge (some versions of MS libc).

Wez Furlong
+2  A: 

You have an endless loop.

$procStatus['running'] will never change unless you put the call to proc_get_status() inside the loop. PHP does not have dynamic properties like JavaScript.

I added the line

$procStatus = proc_get_status( $process );

right after the sleep() and it works fine.

Lucky