实例代码
Swoole\Runtime::setHookFlags(SWOOLE_HOOK_ALL); Swoole\Coroutine\run(function () { $descriptorspec = array( 0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"), ); $process = proc_open('unknown', $descriptorspec, $pipes); var_dump($pipes); var_dump(fread($pipes[2], 8192)); $return_value = proc_close($process); echo "command returned $return_value\n"; });
使用proc_open
,传入了3个描述信息:
fd
为 0的流是标准输入,可以在主进程内向这个流写入数据,子进程就可以得到数据fd
为 1的流是标准输出,这里可以得到执行命令的输出内容fd
为 2的pipe stream
就是stderr
,读取stderr
就能拿到错误信息输出
使用fread
就可以拿到标准错误流输出的内容。
htf@htf-ThinkPad-T470p:~/workspace/swoole/examples/coroutine$ php proc_open.php array(3) { [0]=> resource(4) of type (stream) [1]=> resource(5) of type (stream) [2]=> resource(6) of type (stream) } string(26) "sh: 1: unknown: not found " command returned 32512 htf@htf-ThinkPad-T470p:~/workspace/swoole/examples/coroutine$