在php代码中需要执行一个shell脚本,希望分别得到它的标准输出(stdout)和错误输出(stderr)。用shell的重定向把标准输出和错误输出放到文件中再用php读取文件,可行,但输出文件的内容传回php后就不再需要,有没有办法把shell的输出重定向到变量?
要执行脚本的功能,无法用php本身完成(执行R语言的脚本),并且需要把常规输出和错误输出都反馈。
shell里面的重定向可以把标准输出和错误输出分别放到文件中,但产生临时文件。
<code class="bash">my_cmd 1>out.txt 2>err.txt</code>
php再读取out.txt和err.txt文件内容进行返回。
这样做的缺点:产生了中间文件。如果shell命令my_cmd执行的时间比较长(可能要几分钟),那么同一用户如果短时间内多次发起http请求使得php执行my_cmd,则需要每次out.txt和err.txt的名字都不同,以免写入冲突。这就产生了很多不必要的中间文件。即使及时删除它们,对于I/O还是有开销,希望能避免。
立即学习“PHP免费学习笔记(深入)”;
php的exec()函数:
<code>string exec ( string $command [, array &$output [, int &$return_var ]] )</code>
里面的参数$output看起来只能得到“运行脚本时屏幕输出”的内容,是stdout或者stdout和stderr的混合,不能分开获得。
在php代码中需要执行一个shell脚本,希望分别得到它的标准输出(stdout)和错误输出(stderr)。用shell的重定向把标准输出和错误输出放到文件中再用php读取文件,可行,但输出文件的内容传回php后就不再需要,有没有办法把shell的输出重定向到变量?
要执行脚本的功能,无法用php本身完成(执行R语言的脚本),并且需要把常规输出和错误输出都反馈。
shell里面的重定向可以把标准输出和错误输出分别放到文件中,但产生临时文件。
<code class="bash">my_cmd 1>out.txt 2>err.txt</code>
php再读取out.txt和err.txt文件内容进行返回。
这样做的缺点:产生了中间文件。如果shell命令my_cmd执行的时间比较长(可能要几分钟),那么同一用户如果短时间内多次发起http请求使得php执行my_cmd,则需要每次out.txt和err.txt的名字都不同,以免写入冲突。这就产生了很多不必要的中间文件。即使及时删除它们,对于I/O还是有开销,希望能避免。
立即学习“PHP免费学习笔记(深入)”;
php的exec()函数:
<code>string exec ( string $command [, array &$output [, int &$return_var ]] )</code>
里面的参数$output看起来只能得到“运行脚本时屏幕输出”的内容,是stdout或者stdout和stderr的混合,不能分开获得。
找到答案了,自问自答一次。
使用proc_open()函数,它能执行一段shell脚本,并把stdout和stderr分别保存,还支持设定stdin:
<code>resource proc_open ( string $cmd , array $descriptorspec , array &$pipes [, string $cwd [, array $env [, array $other_options ]]] )</code>
参考代码:
<code class="php">$descriptorspec = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w") // stderr
);
$cmd = 'Rscript hello.r'; // 替换为你要执行的shell脚本
$proc = proc_open($cmd, $descriptorspec, $pipes, null, null);
// $proc为false,表明命令执行失败
if ($proc == false) {
// do sth with HTTP response
} else {
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$status = proc_close($proc); // 释放proc
}
$data = array(
'code' => 0,
'msg' => array(
'stdout' => $stdout,
'stderr' => $stderr,
'retval' => $status
)
);
// do sth with $data to HTTP response</code>
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号