logrotate 在 postrotate 脚本之后压缩文件
我有一个应用程序每天生成一个非常重的大日志文件(每天约 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在此添加此信息,以防其他人在实际搜索想要在压缩完成后在文件上运行脚本的方法时遇到此线程。
如上所述,使用 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.
即使共享脚本生效,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.
postrotate
脚本在压缩发生之前运行:来自 logrotate 的手册页在任何情况下,您都可以使用
delaycompress
选项将压缩推迟到下一次循环。The
postrotate
script does run before compression occurs: from the man page for logrotateIn any case, you can use the
delaycompress
option to defer compression to the next rotation.@Hasturkun - 除非他们的声誉首先高于 50,否则无法添加评论。
要确保 logrotate 会做什么,请
任何内容,以及 -f:强制它运行
-v verbose 标志
使用共享脚本进行 postrotate 的配置
显示以下步骤:
@Hasturkun - One cannot add a comment unless their reputation is first above 50.
To make sure of what logrotate will do, either
anything, and -f: force it to run
the -v verbose flag
With a configuration that uses a sharedscript for postrotate
Shows the following steps: