logrotate 在 postrotate 脚本之后压缩文件

发布于 2024-12-02 09:16:51 字数 408 浏览 1 评论 0原文

我有一个应用程序每天生成一个非常重的大日志文件(每天约 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
}

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

谢谢 托马斯

I have an application generating a really heavy big log file every days (~800MB a day), thus I need to compress them but since the compression takes time, I want that logrotate compress the file after reloading/sending HUP signal to the application.

/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
}

Is it already the case that the compression takes place after the postrotate (which would be counter-intuitive)?
If not Can anyone tell me if it's possible to do that without an extra command script (an option or some trick)?

Thanks
Thomas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧竹 2024-12-09 09:16:51

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

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

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

Adding this info here in case of anyone else that comes across this thread when actually searching for wanting a way to run a script on a file once compression has completed.

As suggested above using postrotate/endscript is no good for that.

Instead you can use lastaction/endscript, which does the job perfectly.

樱&纷飞 2024-12-09 09:16:51

即使共享脚本生效,postrotate 脚本始终压缩之前运行。因此,哈斯塔昆对第一个答案的补充回应是不正确的。当sharedscripts生效时,后旋转之前执行的唯一压缩是针对由于延迟压缩而遗留下来的旧的未压缩日志。对于当前日志,始终在运行postrotate脚本之后执行压缩。

The postrotate script always runs before compression even when sharedscripts is in effect. Hasturkun's additional response to the first answer is therefore incorrect. When sharedscripts is in effect the only compression performed before the postrotate is for old uncompressed logs left lying around because of a delaycompress. For the current logs, compression is always performed after running the postrotate script.

盛装女皇 2024-12-09 09:16:51

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

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

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

The postrotate script does run before compression occurs: from the man page for logrotate

The next section of the config files defined how to handle the log file
/var/log/messages. The log will go through five weekly rotations before
being removed. After the log file has been rotated (but before the old
version of the log has been compressed), the command /sbin/killall -HUP
syslogd will be executed.

In any case, you can use the delaycompress option to defer compression to the next rotation.

樱花坊 2024-12-09 09:16:51

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

要确保 logrotate 会做什么,请

  1. 使用 -d: debug 测试但不执行的配置来测试您的配置
    任何内容,以及 -f:强制它运行
  2. ,或者您可以使用以下命令执行 logrotate
    -v verbose 标志

使用共享脚本进行 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

@Hasturkun - One cannot add a comment unless their reputation is first above 50.

To make sure of what logrotate will do, either

  1. test your configuration with, -d: debug which tests but does not do
    anything, and -f: force it to run
  2. or you can execute logrotate with
    the -v verbose flag

With a configuration that uses a sharedscript for postrotate

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

Shows the following steps:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文