How to background a process via proc_open and have access to STDIN?

蓝咒 提交于 2020-01-30 05:41:52

问题


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'),
);


来源:https://stackoverflow.com/questions/9445815/how-to-background-a-process-via-proc-open-and-have-access-to-stdin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!