Linux 在 postrotate 脚本之后 logrotate 压缩文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7271945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:59:15  来源:igfitidea点击:

logrotate compress files after the postrotate script

linuxshellloggingcompressionlogrotate

提问by Thomas

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.

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

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

谢谢托马斯

采纳答案by Hasturkun

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

postrotate脚本确实在压缩发生之前运行:来自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.

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

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

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

回答by JW-padded-to-three-chars

The postrotatescript alwaysruns beforecompression even when sharedscriptsis in effect. Hasturkun's additional response to the first answer is therefore incorrect. When sharedscriptsis in effect the only compression performed before the postrotateis for old uncompressed logs left lying around because of a delaycompress. For the current logs, compression is alwaysperformed afterrunning the postrotatescript.

postrotate脚本始终运行之前,即使压缩sharedscripts生效。因此,Hasturkun 对第一个答案的补充回应是不正确的。当sharedscripts生效的前执行的唯一压缩postrotate是原来的未压缩日志遗落因为周围delaycompress。对于当前的日志,压缩始终执行运行postrotate脚本。

回答by Mark

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/endscriptis no good for that.

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

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

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

回答by Russell E Glaue

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

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

To make sure of what logrotate will do, either

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

  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
  1. 测试您的配置,-d:调试它测试但不做任何事情,和-f:强制它运行
  2. 或者您可以使用 -v 详细标志执行 logrotate

With a configuration that uses a sharedscript for postrotate

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