logrotate日志分割

0. 环境

logrotate is designed to ease administration of systems that generate large numbers of log files.
It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be
handled daily, weekly, monthly, or when it grows too large.

logrotate旨在简化生成大量日志文件的系统的管理。
它允许自动分割、压缩、删除和邮寄日志文件。每个日志文件可以每天、每周、每月进行处理,也可以在它变得太大时进行处理。

centos7x默认mini安装自带logrotate,也可以yum直接安装

os版本
CentOS Linux release 7.5.1804logrotate-3.8.6

1. 主要配置文件

#  rpm -ql  logrotate
/etc/cron.daily/logrotate
/etc/logrotate.conf
/etc/logrotate.d
/var/lib/logrotate
/var/lib/logrotate/logrotate.status

1.1 /etc/cron.daily/logrotate

logrotate每天通过cron定时触发

#!/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

1.2 /etc/logrotate.conf

logrotate配置文件默认触发周期是一周,不过如nginx一样提供include的方式,可以配置多个不同的配置文件

include /etc/logrotate.d

1.3 /etc/logrotate.d

如:下方testlog则是自己测试编写的配置文件
# ll
total 20
-rw-r--r--. 1 root root  91 Apr 11  2018 bootlog
-rw-r--r--. 1 root root 224 Apr 11  2018 syslog
-rw-r--r--  1 root root 156 Jun  4 17:23 testlog
-rw-r--r--. 1 root root 100 Apr 11  2018 wpa_supplicant
-rw-r--r--. 1 root root 100 Apr 13  2018 yum

1.4 /var/lib/logrotate/logrotate.status

可查询每项配置触发时间点:
"/var/log/yum.log" 2020-4-3-13:0:0
"/var/log/boot.log" 2020-5-12-3:44:1
"/var/log/wtmp" 2020-4-3-13:0:0
"/var/log/spooler" 2020-5-31-3:18:1
"/var/log/btmp" 2020-6-1-3:45:1
"/var/log/maillog" 2020-5-31-3:18:1
"/home/root-nginx/nginx/logs/access.log" 2020-6-4-17:23:28
"/var/log/wpa_supplicant.log" 2020-4-3-13:0:0
"/var/log/secure" 2020-5-31-3:18:1
"/var/log/messages" 2020-5-31-3:18:1
"/var/log/cron" 2020-5-31-3:18:1

2. 配置信息

1. 配置参数:
daily:日志文件每天轮换【yearly 年,weekly 周,monthly 月】
rotate count:日志分割数;如果为0,不分割,删除旧日志文件
delaycompress:与compress结合使用时有效,将上一个日志文件的压缩推迟到下一个循环
compress:旧的日志文件使用gzip压缩
missingok:如果日志文件丢失,请转到下一个日志文件,而不发出错误消息
notifempty:如果日志为空,则不要分割日志
create mode owner group, create owner group:指定分割文件属性【如create 0644 root root】
postrotate/endscript:postrotate和endscript之间的行(这两行必须单独出现在行上)在日志文件分割之后执行
dateext:添加像YYYYMMDD这样的日期扩展名

2. 执行参数:
-v, --verbose :打开详细模式。
-f, --force :告诉logrotate强制分割,即使它不认为这是必要的。
-d, --debug :调试模式下,不会对日志或logrotate状态文件进行任何更改
-m, --mail <command> :告诉logrotate在发送日志时使用哪个命令。

3. man中的例子:
       /var/log/messages {
           rotate 5
           weekly
           postrotate
               /usr/bin/killall -HUP syslogd
           endscript
       }

       "/var/log/httpd/access.log" /var/log/httpd/error.log {
           rotate 5
           mail www@my.org
           size 100k
           sharedscripts
           postrotate
               /usr/bin/killall -HUP httpd
           endscript
       }

       /var/log/news/* {
           monthly
           rotate 2
           olddir /var/log/news/old
           missingok
           postrotate
               kill -HUP `cat /var/run/inn.pid`
           endscript
           nocompress
       }

更多信息请从man中读取。

3. 测试分割

测试分割nginx的access日志:
/home/root-nginx/nginx/logs/access.log {
    daily
    rotate 5
    delaycompress
    missingok
    notifempty
    create 0644 root root
    postrotate
        /bin/kill -USR1 `cat /home/root-nginx/nginx/logs/tt.pid 2>/dev/null` 2>/dev/null || true
    endscript
}

kill -USR1 pid即kill -10 pid
nginx提供-10 pid的信号去重新打开日志文件
如果某些软件没有类似功能,可以添加copytruncate参数,但在分割时可能会丢失少部分日志

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值