缘起
使用如下配置文件,由我的安装脚本将配置文件移动至/etc/logrotate.d目录。
#addonlog
/usr/etc/jkzuc/configs/AddOns/*/log/*.log
{
su root adm
rotate 5
minsize 1k
maxsize 3k
daily
dateext
copytruncate
missingok
notifempty
}
(为了测试更快改了1k和3k)
但是我发现日志文件超出最大限制和一天之后都没有回滚😓😓😓
排查
阶段1
首先使用 logrotate -v /etc/logrotate.d/addonlog
手动运行并显示日志,发现是配置文件addonlog的权限不对。
后面测试确实只能使用0644或者0444权限,777都不行。并且文件所有者必须是root。
修改完上面的内容后再次手动执行测试,生效了。
阶段2
然后就继续写日志超过设置的大小,等待自动执行回滚。
·····10分钟后(我设置了5分钟执行一次)····
日志并没有被回滚。
于是就想看看自动执行有没有生效--->
logrotate的执行时机是在crontable中配置的,具体不讲。总之就是这个文件里配置了系统的一些定时任务。
crontable配置: 第11行的意思是每5分钟运行一次/etc/cron.hourly目录下的脚本,其中就包含运行logrotate。 (5分种是为了测试方便改的)
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
*/5 * * * * root cd / && run-parts --report /etc/cron.hourly
00 */4 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
07 12 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
13 12 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
tail /var/log/syslog
查看日志确实执行了,但日志就是不回滚。
再次手动运行查看日志
竟然是因为已经有相同后缀的回滚文件然后就不回滚了,+_+!
原因是因为这个配置:dateext
在 logrotate
中,dateext
是一个配置选项,用于在轮转日志文件时添加日期扩展。启用 dateext
后,logrotate
会在轮转后的归档文件名中添加当前日期,以便更好地区分不同时间段的日志文件。
查了下也没有参数可以设置,只能精确到日。 也就是说一天只能回滚一次了。。
删掉就好了。 删掉后缀会以1.2.3.4的形式递增。