【问题标题】:How to force a program to write log into standard output In spite of redirection?尽管重定向,如何强制程序将日志写入标准输出?
【发布时间】:2023-02-02 16:46:22
【问题描述】:

背景

今天我用这个命令尝试 perconna 的 innobackupex:

innobackupex --stream=xbstream /root/backup/ > /root/backup/backup.xbstream

但是,我注意到日志显示在我的终端上:

...
IMPORTANT: Please check that the backup run completes successfully.
           At the end of a successful backup run innobackupex
           prints "completed OK!".

230202 15:52:24 Connecting to MySQL server host: 127.0.0.1, user: root, password: set, port: 3306, socket: not set
...

混乱

重定向> 不应该将标准输出写入/root/backup/backup.xbstream 吗? 为什么我仍然可以在终端上看到日志?

【问题讨论】:

    标签: linux


    【解决方案1】:

    请注意,Bash 使用不同的流来输出错误和已处理的数据:stdoutstderr。进行这种分离是为了防止警告和错误消息干扰输出数据的格式(例如,如果要通过管道转发)。

    # redirect the command output, but omit warnings and errors
    # (these will end up on stderr, which is written to the console by default)
    mycommand > /some/path/myfile
    
    # redirect warnings and errors only
    mycommand 2> /some/path/myfile
    
    # redirect both
    mycommand &> /some/path/myfile
    

    有关更多详细信息,请参阅GNU's official bash documentation on redirections

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-04
      • 2010-10-14
      • 2016-06-01
      • 2016-04-26
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      相关资源
      最近更新 更多