我有一个简单的脚本,它输出一堆日志到屏幕,我将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