【问题标题】:Unable to log output of "apachectl configtest" to a file无法将“apachectl configtest”的输出记录到文件中
【发布时间】:2016-06-02 21:21:21
【问题描述】:

我想将命令“apachectl configtest”的输出记录到文件中。所以我尝试了以下命令:

root@ubuntu:~# echo $(apachectl -t) >> /tmp/apache_config_check
[Sun Feb 21 14:35:23.614947 2016] [proxy_html:notice] [pid 29249] AH01425: I18n support in mod_proxy_html requires mod_xml2enc. Without it, non-ASCII characters in proxied pages are likely to display incorrectly.
Syntax OK

但它给出了空输出:

root@ubuntu:~# cat /tmp/apache_config_check 

root@ubuntu:~# 

我还尝试了命令本身的变体:

root@ubuntu:~# apachectl -t >> /tmp/apache_config_check

以及 tee 的变体:

root@ubuntu:~# $(apachectl -t) | tee /tmp/apache_config_check

没有运气。

我真的不知道任何其他管道输出的方法,也不知道上述命令失败的原因。这是基本的东西吗?

【问题讨论】:

    标签: linux apache apache2 tee piping


    【解决方案1】:

    问题在于apachectl 将其输出发送到 STDERR 而不是 STDOUT。因此,您需要将 STDERR 重定向如下:

    apachectl -t > /tmp/apache_config_check 2>&1
    

    2>&1 是 shell 脚本魔术,说“将文件描述符 2 (STDERR) 上的输出重定向到文件描述符 1 (STDOUT)”。

    【讨论】:

    • 感谢您的解决方案+清晰的解释!有没有办法确定命令是使用 STDERR 还是 STDOUT?还是应该在手册页中提及?
    • 要真正确定它发送到哪里,必须对其进行测试。例如apachectl -t > STDOUT.output 2> STDERR.output 将输出分成两个不同的文件。一般来说,常规输出应该到 STDOUT,错误信息到 STDERR。通常,它会在手册页中提及。在这种特殊情况下,它很复杂,因为apachectl 脚本只调用/usr/sbin/httpd -t,并且在手册页中没有任何地方解释该输出到 STDERR。
    最近更新 更多