How to background a process via proc_open and have

2020-04-10 15:58发布

问题:

I'm happily using proc_open to pipe data into another PHP process. something like this

$spec = array (
    0 => array('pipe', 'r'),
    // I don't need output pipes
);
$cmd = 'php -f another.php >out.log 2>err.log';
$process = proc_open( $cmd, $spec, $pipes );
fwrite( $pipes[0], 'hello world');
fclose( $pipes[0] );
proc_close($process);

In the other PHP file I echo STDIN with:

echo file_get_contents('php://stdin');

This works fine, but not when I background it. Simply by appending $cmd with & I get nothing from STDIN. I must be missing something fundamental.

It also fails with fgets(STDIN)

Any ideas please?

回答1:

You can't write to STDIN of a background process (at least, not in the normal way).

This question on Server Fault may give you some idea of how to work around this problem.

Unrelated: you say do don't need outputs in the spec, yet you specify them im your $cmd; you can write $spec like this:

$spec = array (
    0 => array('pipe', 'r'),
    1 => array('file', 'out.log', 'w'), // or 'a' to append
    2 => array('file', 'err.log', 'w'),
);