【问题标题】:logrotate compress files after the postrotate script在 postrotate 脚本之后 logrotate 压缩文件
【发布时间】:2011-11-08 11:22:07
【问题描述】:

我有一个应用程序每天生成一个非常大的日志文件(每天约 800MB),因此我需要压缩它们,但由于压缩需要时间,我希望 logrotate 在重新加载/发送 HUP 信号后压缩文件应用程序。

/var/log/myapp.log {
    rotate 7
    size 500M
    compress
    weekly
    postrotate
        /bin/kill -HUP `cat /var/run/myapp.pid 2>/dev/null` 2>/dev/null || true
    endscript
}

压缩是否已经发生在后旋转之后(这会违反直觉)? 如果没有,谁能告诉我是否可以在没有额外的命令脚本(一个选项或一些技巧)的情况下做到这一点?

谢谢 托马斯

【问题讨论】:

    标签: linux shell logging compression logrotate


    【解决方案1】:

    在此处添加此信息,以防其他任何人在实际搜索压缩完成后想要在文件上运行脚本的方法时遇到此线程。

    如上所述,使用 postrotate/endscript 对此没有好处。

    相反,您可以使用 lastaction/endscript,它可以完美地完成这项工作。

    【讨论】:

    • 你就是那个男人。为我节省了至少 30 分钟的谷歌搜索/阅读时间。
    • 您可能想创建一个单独的问题并回答它,您将获得声誉并提高此回答线程的质量。
    【解决方案2】:

    @Hasturkun - 除非他们的声誉首先超过 50,否则无法添加评论。

    为了确定 logrotate 会做什么,要么

    1. 使用 -d 测试您的配置:调试哪些测试但不执行 任何东西,-f: 强制它运行
    2. 或者您可以执行 logrotate -v 详细标志

    配置使用共享脚本进行 postrotate

    $ logrotate -d -f <logrotate.conf file>
    

    显示以下步骤:

    rotating pattern: /tmp/log/messages /tmp/log/maillog /tmp/log/cron
    ...
    renaming /tmp/log/messages to /tmp/log/messages.1
    renaming /tmp/log/maillog to /tmp/log/maillog.1
    renaming /tmp/log/cron to /tmp/log/cron.1
    running postrotate script
    <kill-hup-script executed here>
    compressing log with: /bin/gzip
    compressing log with: /bin/gzip
    compressing log with: /bin/gzip
    

    【讨论】:

    • 假设您指的是我的(撤回的)评论,IIRC(从那时起已经有一段时间了)我在出现错误时检查了 logrotate 源。这确实是错误的,行为不受影响。无论如何,测试您的配置可能是个好主意。
    【解决方案3】:

    postrotate 脚本总是 运行 before 压缩,即使 sharedscripts 有效。因此,Hasturkun 对第一个答案的附加反应是不正确的。当 sharedscripts 生效时,在 postrotate 之前执行的唯一压缩是针对由于 delaycompress 而留下的旧未压缩日志。对于当前日志,始终在运行 postrotate 脚本之后执行压缩。

    【讨论】:

    • 我自己发现了这个。同意@Hasturkun 修改或删除他的评论。
    • 如果这是一条评论,我可能会在不到八个月的时间内回复
    【解决方案4】:

    postrotate 脚本在压缩发生之前运行:来自logrotate 的手册页

    配置文件的下一部分定义了如何处理日志文件 /var/log/消息。日志将经过五周轮换之前 被移除。在日志文件被轮换之后(但在旧的 日志版本已被压缩),命令 /sbin/killall -HUP syslogd 将被执行。

    在任何情况下,您都可以使用delaycompress 选项将压缩推迟到下一次旋转。

    【讨论】:

    • 谢谢,我应该更仔细地阅读手册页......虽然这些信息也应该在压缩选项说明下。
    • 所有读者请注意,根据@jw-padded-to-three-chars 下面的回答,上述评论似乎是错误的,请参阅后者。
    • @Thomas:评论基于手册页The sharedscripts means that the postrotate script will only be run once (after the old logs have been compressed), not once for each log which is rotated. 中的这句话,这似乎是我对那句话的误解。 postrotate 脚本总是在压缩当前日志之前运行。 (delaycompress 选项不受sharedscripts 的任何影响,除了在prerotate 脚本运行之前发生)
    • 对 logrotate 完成的顺序感兴趣的人会发现 logrotate -d 很有用 - 它可以准确地告诉您正在发生的事情。您可能还想使用-f
    • 那么手册是什么意思呢?对我来说,“压缩旧日志后”部分似乎不仅令人困惑,而且是错误的。
    猜你喜欢
    • 2015-12-05
    • 2020-08-06
    • 2011-05-28
    • 2016-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 2018-02-13
    相关资源
    最近更新 更多