Php system()/ exec()不返回输出 [英] Php system() / exec() don't return output

查看:1167
本文介绍了Php system()/ exec()不返回输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于像'ls'这样的常用命令,exec()工作正常,例如:

For common commands like 'ls', exec() works fine, eg:

exec('ls',$output,$retval);
var_dump($output,$retval);
// $output contains an array of filenames, and retval === 0

但是当尝试执行另一个程序时,我无法得到输出:

But when trying to execute another program, I can't get the output:

exec('some_command --a_parameter',$output,$retval);
var_dump($output,$retval);
// $output contains an empty array, end $retval === 0

命令在从命令行直接执行命令时打印一些行。
我知道命令已经成功,因为结果(一些文件更新,数据添加等),但我看不到输出。

This command prints some lines when executing it directly from the command-line though. I know the command has been successful because of the results (some files updated, data added, etc), and yet I can't see the output.

任何想法?

推荐答案

这听起来像程序正在向标准错误而不是标准输出输出警告。 exec 将只捕获标准输出。我不知道确定标准错误总是发送到apache错误日志,但似乎可能。

It sounds like the program is outputting its warnings to standard error rather than standard output. exec will only catch standard output. I don't know for certain that standard error is always sent to the apache error log, but it seems likely.

如果您不需要与非* nix系统,您可以通过在命令中附加 2>& 1 将标准错误重定向到标准输出:

If you don't need compatibility with non-*nix systems, you can redirect standard error to standard output by appending 2>&1 to the command:

exec('some_command --option 2>&1', $output, $ret);

这应该使警告可用于您的PHP程序,并防止不必要的日志记录。

This should both make the warnings available to your php program and prevent unnecessary logging.

这篇关于Php system()/ exec()不返回输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆