logrotate详情和坑点

本文详细介绍了logrotate配置参数,包括compress、nocompress、copytruncate等选项的用法,以及daily、weekly、monthly等周期设置。特别强调了logrotate的坑点,如默认每天运行一次可能不满足实时需求,size参数的设定和周期的关系,以及dateformat的限制。还提到了日志切割后继续写入的问题,解决方案是添加sharedscripts和postrotate参数。总结中指出,logrotate功能虽好,但实际使用时可能需要结合自定义脚本和计划任务来确保效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

logrotate 配置参数

        compress                        通过gzip压缩转储以后的日志
        nocompress                      不压缩
        copytruncate                    用于还在打开中的日志文件,把当前日志备份并截断
        nocopytruncate                  备份日志文件但是不截断
        create mode owner group         转储文件,使用指定的文件模式创建新的日志文件
        nocreate                        不建立新的日志文件
        delaycompress 和 compress        一起使用时,转储的日志文件到下一次转储时才压缩
        nodelaycompress                 覆盖 delaycompress 选项,转储同时压缩。
        errors address                   专储时的错误信息发送到指定的Email 地址
        ifempty                         即使是空文件也转储,这个是 logrotate 的缺省选项。
        notifempty                      如果是空文件的话,不转储
        mail address                    把转储的日志文件发送到指定的E-mail 地址
        nomail                          转储时不发送日志文件
        olddir directory                转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
        noolddir                        转储后的日志文件和当前日志文件放在同一个目录下
        prerotate/endscript             在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
        postrotate/endscript            在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行
        daily                           指定转储周期为每天
        weekly                          指定转储周期为每周
        monthly                         指定转储周期为每月
        rotate count                    指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
        tabootext [+] list 让logrotate   不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~
        size size                       当日志文件到达指定的大小时才转储,bytes(缺省)及KB(sizek)或MB(sizem)

坑点
本人亲测

坑一:

logrotate 使用系统cron运行 默认是每天运行一次

所以你设置各种参数,有可能还是不运行的,比如每小时检测日志大小切割,除非你添加每小时运行的计划任务

脚本路径

/etc/cron.daily/logrotate 

解析

#!/bin/sh

/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf ##运行日志轮转命令
EXITVALUE=$?  ##这段是监测轮转是否正常完成,不正常写入系统日志
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

坑二:

logrotate的三种size方式
请时刻记住不设置计划任务的话 默认每天执行一次日志轮转命令

minsizi   两个条件都满足 ,大小到了,并且执行了日志切割命令 (周期内执行0、1次)
maxsizi   任一条件 (周期内执行1、n次)
sizi      大小到了就执行 ,但是要设置每分钟执行日志切割命令(设置后,周期参数自动失效)(无周期概念)

坑三:

logrotate的轮转周期
会影响轮转命令的执行,
不添加 sizi参数时,为执行命令的最小周期
添加后,根据具体sizi参数变化

坑四

logrotate的dateformat参数

只支持 %Y %m %d %H %s  这五种时间格式

dateformat -%Y%m%d%H.%s
效果 2021072214.1626933601

坑五

切割后继续往旧日志里写日志
系统日志相关

   添加这3个参数
   sharedscripts
   postrotate
        /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
   endscript

总结 :个人认为有点鸡肋的功能,你设置了其中的参数,但他不一定生效
不如个人写个shell脚本去检测,清理,一系列操作 添加个计划任务,但是没办法,系统自带的 肯定也是最稳定的,还是人太菜,可能没玩明白吧

示例:

{ 
    compress                ##gizp 压缩
    delaycompress           ##转储后下一次再压缩
    rotate 6                ##备份和压缩备份一共6个
    create 0600 root root   ##创建原文件源属性
    copytruncate            ##无间断转储
    missingok               ##忽略错误
    dateext                 ##启用时间格式
    dateformat -%Y%m%d%H.%s ##自定义时间格式
    sharedscripts           ##轮转后的脚本段 (作用为,告知syslogd,使用新文件写入)发现配置后继续写入旧文件时配置该段
    postrotate              ##轮转后的脚本段
         /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript               ##轮转后的脚本段
}
### Logrotate Configuration Overview In Linux systems, `logrotate` plays an essential role in managing logs efficiently. Two primary configuration aspects exist within the context of log management: `/etc/logrotate.conf` and files located inside the directory `/etc/logrotate.d/`. Each serves distinct purposes yet collaborates seamlessly to ensure effective handling of system logging. #### Understanding /etc/logrotate.conf The file `/etc/logrotate.conf` acts as the global configuration for all log rotation activities on a machine. This central location defines default settings applicable across various services unless overridden locally. Common parameters include specifying where rotated logs are stored, defining frequency (daily, weekly, monthly), setting up compression options, controlling retention policies through count limits, and more[^1]. For instance: ```bash # Sample content from /etc/logrotate.conf weekly rotate 4 compress delaycompress notifempty create 0644 root adm include /etc/logrotate.d ``` This excerpt sets rotations every week (`weekly`) while keeping four old versions (`rotate 4`). Compression occurs after one cycle due to `delaycompress`, ensuring current archives remain uncompressed until their next scheduled turn. Logs empty during inspection won't trigger unnecessary processing thanks to `notifempty`. #### Exploring /etc/logrotate.d/ Conversely, individual service-specific configurations reside under directories like `/etc/logrotate.d/`. These separate entries allow granular control over particular applications without modifying core directives set globally via `logrotate.conf`. Developers or administrators add custom rules here tailored specifically towards unique requirements per application or daemon involved. An example entry might look similar to below when targeting Apache HTTP Server's access logs: ```bash # Content typically found within /etc/logrotate.d/apache2 /var/log/apache2/*.log { daily missingok rotate 14 compress delaycompress notifempty create 640 root adm } ``` Here, specific instructions apply only to web server-related records managed separately despite inheriting broader guidelines established earlier. --- --related questions-- 1. How does configuring log expiration impact disk space utilization? 2. What security benefits arise from properly rotating sensitive logs regularly? 3. Can third-party tools integrate with logrotate mechanisms effectively? 4. In what scenarios would overriding global logrotate settings become necessary? 5. Are there performance implications associated with frequent versus infrequent log rotations?
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大鹅i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值