如何检索 PHP exec() 错误响应?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/345794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to retrieve PHP exec() error responses?
提问by Matt
Below is the command I tried executing, without success:
下面是我尝试执行的命令,但没有成功:
exec('ln -s ' . PLUGIN_DIR . '/.htaccess ' . ABSPATH . '/.htaccess');
When you add a die() at the end, it catches that there's an error:
当你在最后添加一个 die() 时,它会发现有一个错误:
exec('ln -s ' . PLUGIN_DIR . '/.htaccess ' . ABSPATH . '/.htaccess') or die('what?!');
For the above exec() statement, a permissions problem is causing the error, but PHP isn't displaying it. How do you display from PHP what error is occurring?
对于上面的 exec() 语句,权限问题导致了错误,但 PHP 没有显示它。你如何从 PHP 显示发生了什么错误?
采纳答案by Eran Galperin
You can receive the output result of the exec functionby passing an optional second parameter:
您可以通过传递可选的第二个参数来接收exec 函数的输出结果:
exec('ln -s ' . PLUGIN_DIR . '/.htaccess ' . ABSPATH . '/.htaccess',$output);
var_dump($output);
回答by S.ov
The $output parameter does not appear to work if the calling program spits output to STDERR.
如果调用程序将输出输出到 STDERR,则 $output 参数似乎不起作用。
A better way to handle this is to redirect the output from exec to a file and then display the contents of that file if there is an error condition.
处理此问题的更好方法是将 exec 的输出重定向到一个文件,然后在出现错误情况时显示该文件的内容。
If $cmd holds the exec command add something like this:
如果 $cmd 持有 exec 命令,则添加如下内容:
$cmd.=" > $error_log 2>&1"
Then examine the contents of the filespec in $error_log for detailed info on why the command failed.
然后检查 $error_log 中 filespec 的内容以获取有关命令失败原因的详细信息。
Also note that if you fork this off with a & at the end of the command, an immediate check of the contents of $error_log may not reveal the log information - the script may check/process the file before the OS has finished.
另请注意,如果您在命令末尾使用 & 将其分叉,则立即检查 $error_log 的内容可能不会显示日志信息 - 脚本可能会在操作系统完成之前检查/处理文件。
回答by Amaynut
The following code will capture both the normal output (from StdOut) and the error output (from SdtErr).
以下代码将捕获正常输出(来自 StdOut)和错误输出(来自 SdtErr)。
exec('ln -s ' . PLUGIN_DIR . '/.htaccess ' . ABSPATH . '/.htaccess' . '2>&1',$output);
var_dump($output);
回答by Bonfix Ngetich
This worked for me in several scenarios:
这在几种情况下对我有用:
ob_start();
exec($cmd . " 2>&1", $output);
$result = ob_get_contents();
ob_end_clean();
var_dump($output);
The space between " 2is important
" 2之间的空格很重要
回答by luciomonter
this worked for me: Append 2>&1 to the end of your command.
这对我有用:将 2>&1 附加到命令的末尾。
Seen here: php exec() not returning error message in output when executing svn command
在这里看到: php exec() 在执行 svn 命令时不会在输出中返回错误消息
Cheers
干杯
回答by David L
If you don't find the error through that second parameter, you can search for the error log of apache, for example in Ubuntu Server 12.10 through the command $ tail /var/log/apache2/error.log I was running a python script from php, whose error was only printed there, and hence I was able to diagnose it. Best regards.
如果通过第二个参数没有找到错误,可以搜索apache的错误日志,例如在Ubuntu Server 12.10中通过命令 $ tail /var/log/apache2/error.log 我正在运行一个python脚本来自 php,它的错误只打印在那里,因此我能够诊断它。此致。
回答by maximusin9
This in not a direct answer to your question, but is very useful, if you want to know what exactly happened by returned error code.
这不是对您问题的直接回答,但如果您想知道返回的错误代码究竟发生了什么,则非常有用。
If error code ($res), returned by command:
如果错误代码($res),由命令返回:
exec('command', $out, $res);
was in range 1-2, 126-165, 255, than it was returned by shell (eg. BASH) invoked from PHP (to execute your command). If returned code is not in this range, than it was returned by your command (not shell).
范围为 1-2、126-165、255,而不是由 PHP 调用的 shell(例如 BASH)返回(以执行您的命令)。如果返回的代码不在此范围内,则它是由您的命令(不是 shell)返回的。
See (for BASH error code descriptions)
请参阅(有关 BASH 错误代码说明)
https://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/exitcodes.html
https://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/exitcodes.html

