微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

linux – 如何在logrotate移动后继续将stdout重定向到文件?

我有一个简单的脚本,它输出一堆日志到屏幕,我将STDOUT传送到一个文件来存储日志.由于这个脚本长时间运行,我需要旋转日志文件,以便将它们放入更小的更易于管理的文件中.

我遇到的问题是,一旦logrotate将当前日志文件移动到新文件中,新创建的日志文件就不再填充日志了.似乎删除原始日志文件后,其文件处理程序将丢失,重定向将不再起作用.

我还发现this post我有同样的问题,并声称可以通过使用>>来修复它.而不是>重定向输出.我测试了他的解决方案,但它对我不起作用.有谁知道如何保持重定向工作?

解决方法

您应该在logrotate配置中使用copytruncate指令来获取此日志文件.

copytruncate Truncate the original log file in place after creating a copy,instead of moving the old log file and optionally creating a new one. It can be used when some program cannot be told to close its logfile and thus might continue writing (appending) to the prevIoUs log file forever. Note that there is a very small time slice between copying the file and truncating it,so some logging data might be lost. When this option is used,the create option will have no effect,as the old log file stays in place

原文地址:https://www.jb51.cc/linux/402424.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐