![尽管有重定向,如何强制程序将日志写入标准输出?](https://linux22.com/image/216469/%E5%B0%BD%E7%AE%A1%E6%9C%89%E9%87%8D%E5%AE%9A%E5%90%91%EF%BC%8C%E5%A6%82%E4%BD%95%E5%BC%BA%E5%88%B6%E7%A8%8B%E5%BA%8F%E5%B0%86%E6%97%A5%E5%BF%97%E5%86%99%E5%85%A5%E6%A0%87%E5%87%86%E8%BE%93%E5%87%BA%EF%BC%9F.png)
背景
今天我正在使用以下命令尝试 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吗?为什么我仍然可以在终端上看到日志?
答案1
请注意,类 Unix 系统中的进程具有不同的错误输出流和已处理数据流:stderr
和stdout
。进行这种分离是为了防止警告和错误消息干扰输出数据的格式(例如,如果要通过管道转发)。
# 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 关于重定向的官方 Bash 文档有关重定向每个流的更多详细信息。