exec()(或类似函数)传回错误输出

Phi*_*ord 2 php error-handling exec

我可以传回执行脚本的输出,但如果脚本错误输出我没有输出错误.

// This is a file that doesn't exists, for testing
$command = './path/to/non/existing/script.sh';

$commandOutput = exec($command, $commandOutput); // works but no error output
//passthru($command, $commandOutput); // works but error output was 127 not file not found
//$commandOutput = escapeshellcmd($command);
echo "The Output:\n|".$commandOutput."|\n";
var_dump($commandOutput);

The Output:

||
Run Code Online (Sandbox Code Playgroud)

我想输出错误信息:

The Output:

|file not found|
Run Code Online (Sandbox Code Playgroud)

如何或者什么功能/参数会这样做?

Vol*_*erK 6

您可以将stderr重定向到stdout,因此exec()等将通过附加2>&1到您的命令来获取错误消息.

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html