加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-13 18:36:58 所属栏目:Linux 来源:网络整理
导读:我有一个简单的脚本,它输出一堆日志到屏幕,我将STDOUT传送到一个文件来存储日志.由于这个脚本长时间运行,我需要旋转日志文件,以便将它们放入更小的更易于管理的文件中. 我遇到的问题是,一旦logrotate将当前日志文件移动到新文件中,新创建的日志文件就不再填
我有一个简单的脚本,它输出一堆日志到屏幕,我将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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读