我很乐意使用 proc_open
将数据通过管道传输到另一个 PHP 进程。
像这样
$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);
在另一个 PHP 文件中,我回显了 STDIN:
echo file_get_contents('php://stdin');
这工作正常,但当我将它设置为背景时却不行。只需将 $cmd
附加到 &
,我就不会从 STDIN 中得到任何信息。我一定错过了一些基本的东西。
它也因 fgets(STDIN)
而失败
有什么想法吗?
最佳答案
您不能写入后台进程的 STDIN(至少,不能以正常方式)。
This question on Server Fault可能会让您了解如何解决此问题。
不相关:你说在规范中不需要输出,但你在你的 $cmd
中指定了它们;你可以这样写 $spec
:
$spec = array (
0 => array('pipe', 'r'),
1 => array('file', 'out.log', 'w'), // or 'a' to append
2 => array('file', 'err.log', 'w'),
);
关于php - 如何通过 proc_open 后台进程并访问 STDIN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49259962/