logrotate命令
logratet命令,主要用来切割日志。
logrotate -v -s ./logrotate-status /etc/logrotate.d/demo # -v 输出命令执行过程中的详细信息 -s 指定输出每次rotate时的state文件路径。
/etc/logrotate.d/demo是指定每次rotate日志时所用的配置文件,内容如下:
/root/hellear_test/demo.txt {
hourly
compress
copytruncate
notifempty
rotate 5 # 只保留5个归档文件
missingok
dateext
dateformat -%Y%m%d.%s # 给归档文件添加后缀名: demo.txt-20200716.1594890921.gz
}
首先必须明确一点: 只有当logrotate -v -s ./logrotate-status /etc/logrotate.d/demo
命令执行的时候,我们的日志文件demo.txt才会根据条件判断是否要rotate。换句话说,只有当logrorate命令执行的时候才会触发demo.txt日志文件文件发生滚动,否则demo.txt日志文件是不会自行滚动的。那么至于lograte命令什么时候执行,执行的频次如何,完全是由用户自己定义,你可以crontab起定时服务去执行logrortate命令,或者通过定时脚本去执行logrorate命令,执行周期完全有自定义:可以一秒执行一次、一分钟执行一次、半个小时执行一次、一个小时执行一次、一天执行一次……
参数解释:
这里重点解释一下用到的几个参数: hourly, minsize , maxsize , size, rotate
1、滚动周期
首先介绍一个最重要的参数 : hourly
hourly表示滚动周期为1小时,即 一个小时内只对日志文件进行一次滚动操作,不管日志文件的大小如何。意思是说:如果在0:00--0:59之内,任意执行一次logrorate命令就可以使demo.txt发生滚动并压缩,但是同一小时内第二次执行logrotate命令就没有作用,因为你将滚动周期设置成了hourly,一个小时内只滚动一次,当logrorate发现当前一小时内已经发生了一次,就不会再次滚动了。只有等到下一个滚动周期1:00--1:59之内,再次执行logrorate命令才会触发滚动。同理适用daily、weekly、monthly、yearly。
注:所谓的滚动操作就是指:
reading config file /etc/logrotate.d/demo # 首先读取指定的滚动配置文件
Handling 1 logs
rotating pattern: /root/hellear_test/demo.txt 102400 bytes (5 rotations) # 超过100k就滚动,只保留5个最新的归档文件
empty log files are not rotated, old logs are removed
considering log /root/hellear_test/demo.txt
log needs rotating # 判断当前日志文件是否需要rotate
rotating log /root/hellear_test/demo.txt, log->rotateCount is 5
Converted ' -%Y%m%d.%s' -> '-%Y%m%d.%s'
dateext suffix '-20200716.1594897250' # 添加归档文件后缀,年、月、日、时间戳(1900年起的毫秒)
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
copying /root/hellear_test/demo.txt to /root/hellear_test/demo.txt-20200716.1594897250 # 将当前日志文件内容全部复制到归档文件中
truncating /root/hellear_test/demo.txt # 然后清空当前日志文件
compressing log with: /bin/gzip # 再对归档文件进行压缩
removing old log /root/hellear_test/demo.txt-20200716.1594882033.gz # 最后删除最旧的一个压缩归档文件(只保留5个)
2、maxsize
那么有没有办法使一个小时内多发次滚动呢?
比如我做了一个定时任务,每分钟执行一次logrotate命令,同时滚动周期设置成hourly。
这个时候就轮到maxsize登场了
root/hellear_test/demo.txt {
hourly
compress
copytruncate
notifempty
rotate 5
maxsize 100k # 将maxsize设置成100k
missingok
dateext
dateformat -%Y%m%d.%s
}
这时由于将maxsize设置成了100k,当执行logrorate指令时,只要demo.txt的大小超过100k,即使时间没到下一个滚动周期内,也会发生滚动。那么同一小时内0:00--0:59就会发生多次滚动。换句话说,如果demo.txt的大小一直没有达到maxsize,那么一个滚动周期就只会发生一次滚动,即当前滚动周期内第一次执行logrorate指令就会触发滚动。以后的59分钟都不会发生滚动。maxsize可以使滚动提前发生,再下一个滚动周期到来之前发生多次滚动。
总结一句话:每满足maxsize就滚动一次,不满足则滚动1次,(每个滚动周期内,n次或1次)
man手册解释:
【maxsize size】 Log files are rotated when they grow bigger than size bytes even before the additionally specified time interval (daily, weekly, monthly, or yearly). The related size option is similar except that it is mutually exclusive with the time interval options, and it causes log files to be rotated without regard for the last rotation time. When maxsize is used, both the size and timestamp of a log file are considered.
3、minsize
对于minsize的功能,如下:
root/hellear_test/demo.txt {
hourly
compress
copytruncate
notifempty
rotate 5
minsize 100k # 将minsize设置成100k
missingok
dateext
dateformat -%Y%m%d.%s
}
minsize表示,执行logrorate命令时,只要当demo.txt文件大小超过100k,才会发生滚动,但是一个滚动周期内只会发生一次滚动。比如logrotate还是每分钟执行一次,在0:33时,demo.txt的大小超过了100k,此时logrotate命令就会使日志发生滚动。然后在当前滚动周期后面的时间里,即使demo.txt大小再次超过100k,也不会再发生滚动,即minsize不能使滚动提前发生。如果demo.txt的大小一直没有达到minsize,那么这个滚动周期内是不会触发滚动的。即如果0:00--0:59之每次执行logrotate指令时,demo.txt的大小都小于100k,那么demo.txt是不会发生滚动的。
总结一句话:满足minsize则滚动一次,不满足则滚动0次,(每个滚动周期内,1次或0次)
man手册解释:
【minsize size】 Log files are rotated when they grow bigger than size bytes, but not before the additionally specified time interval (daily, weekly, monthly, or yearly). The related size option is similar except that it is mutually exclusive with the time interval options, and it causes log files to be rotated without regard for the last rotation time. When minsize is used, both the size and timestamp of a log file are considered.
4、size
对于size的功能,如下:
root/hellear_test/demo.txt {
compress
copytruncate
notifempty
rotate 5
size 100k # 将size设置成100k
missingok
dateext
dateformat -%Y%m%d.%s
}
首先明确一点:size参数跟滚动周期参数:hourly、daily、monthly、yearly完全是互斥的。即是说,一旦设置了size参数,滚动周期参数就自动无效了,只要每次执行logrotate指令时,demo.txt文件大小超过size,就会触发一次滚动,没有滚动周期一说了。
总结一句话:满足size就滚动一次,(没有滚动周期概览,n次)
man手册解释:
【size size】 Log files are rotated only if they grow bigger then size bytes. If size is followed by k, the size is assumed to be in kilobytes. If the M is used, the size is in megabytes, and if G is used, the size is in gigabytes. So size 100, size 100k, size 100M and size 100G are all valid.
5、rotate
首先明确一点:这个参数一定要设置,如果不设置则默认为1 , 那么只会保存一个滚动压缩的文件和一个正在输出日志文件demo.txt。
如果设置了rotate 5 ,那么只会保留5个滚动后的归档文件,时间比较旧归档文件就会被删除。注:永远只会保留5个归档文件,而不是每个滚动周期只保留5个归档文件。即每次执行logrotate指令时,会自动判断当期总的归档文件个数是否超过了5,如果超过了,再滚动完成后就会自动删除最老的一个。如果是rotate 0 的话则不会有任何归档文件保存下来。
man手册解释:
【rotate count】 Log files are rotated count times before being removed or mailed to the address specified in a mail directive. If count is 0, old versions are removed rather than rotated.
6、maxage
自动删除掉超过maxage指定天数的归档文件。
man手册解释:
【maxage count】 Remove rotated logs older than <count> days. The age is only checked if the logfile is to be rotated. The files are mailed to the configured address if maillast and mail are configured.