how to get output of proc_open()

Bobby Stenly picture Bobby Stenly · May 16, 2011 · Viewed 25.3k times · Source

I've tried to get output from proc_open method in php, but, when I print it, I got empty.

$descriptorspec = array(
    0 => array("pipe", "r"),
    1 => array("pipe", "w"),
    2 => array("file", "files/temp/error-output.txt", "a")
);

$process = proc_open("time ./a  a.out", $descriptorspec, $pipes, $cwd);

as long as I know, I can get the output with stream_get_contents()

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

But I can't do that.. any suggestion?

Thx before...

Answer

e.dan picture e.dan · May 16, 2011

Your code more or less works for me. time prints its output to stderr so if you're looking for that output, look in your file files/temp/error-output.txt. The stdout pipe $pipes[1] will only contain the output of the program ./a.

My repro:

[edan@edan tmp]$ cat proc.php 

<?php

$cwd='/tmp';
$descriptorspec = array(
    0 => array("pipe", "r"),
    1 => array("pipe", "w"),
    2 => array("file", "/tmp/error-output.txt", "a") );

$process = proc_open("time ./a a.out", $descriptorspec, $pipes, $cwd);

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

?>

[edan@edan tmp]$ php proc.php 

a.out here.

[edan@edan tmp]$ cat /tmp/error-output.txt

real    0m0.001s
user    0m0.000s
sys     0m0.002s