【问题标题】:How can I fix "/etc/cron.daily/logrotate: gzip: stdin: file size changed while zipping"?如何修复“/etc/cron.daily/logrotate: gzip: stdin: file size changed while zipping”?
【发布时间】:2015-09-18 06:17:20
【问题描述】:

在过去的几天里,我每天都会收到来自 cron 的 logrotate 任务的邮件:

/etc/cron.daily/logrotate:

gzip: stdin: 压缩时文件大小改变

我该如何解决?

谢谢, 吉安·马可。

【问题讨论】:

标签: email logrotate


【解决方案1】:

这是a blog post in French,它提供了一个解决方案。

英文版可以看this bug report

总结一下:

  1. 首先,您必须在脚本/etc/cron.daily/logrotate 中添加--verbose 选项,以便在下次运行时获得更多信息,以确定导致问题的轮换日志。

    #!/bin/sh
    
    test -x /usr/sbin/logrotate || exit 0
    /usr/sbin/logrotate --verbose /etc/logrotate.conf`
    
  2. 接下来,您必须在 logrotate 配置中添加 delaycompress 选项。

    和例子一样,我在 /etc/logrotate.d/nginx 中添加了 nginx 的 logrotate 配置:

    /var/log/nginx/*.log {
        daily
        missingok
        rotate 14
        compress
        delaycompress
        notifempty
        create 0640 www-data adm
        sharedscripts
        ...
    }
    

【讨论】:

  • 您可以立即运行 logrotate 还是必须等待它再次运行?如何将 delaycompress 添加到 logrotate 配置中?能举个例子吗?
  • @LanceHolland 您可以使用以下命令再次运行它: /usr/sbin/logrotate /etc/logrotate.conf 但是,根据您的 logrotate 设置(频率、最小大小等),条件可能不充分导致gzip问题。我认为最好添加详细信息,然后让它按照每日计划运行。
  • delaycompress 只会在某些情况下为您提供帮助。看我的回答。
【解决方案2】:

upstart 将在it notices that the file is deleted 时关闭(并重新打开)其日志文件。但是,如果您查看what gzip does,您会发现它在写入输出文件之前不会删除该文件。这意味着总是存在一种竞争条件,其中日志行可能会丢失正在写入的行日志gzipping

您可以使用gzip --quiet 禁用警告,但这并不能掩盖您可能仍会丢失日志行的事实。

这意味着delaycompress 不是对此的通用修复。这是针对特定问题的特定修复。

真正的解决方案可能是delaycompress 和能够向进程发送信号的组合。它会使比赛条件在实践中消失(除非你每秒旋转多次:))。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 2019-10-06
    • 2013-11-23
    • 2022-12-24
    • 1970-01-01
    相关资源
    最近更新 更多