注意:我这个配置里没有设置多久切割一次,但是它也是一天切割一次,因为当/etc/logrotate.d/tomcat这个文件没有配置时,默认是每天定时执行/etc/cron.daily/logrotate这个文件,这个文件是一天一次的。
#强制执行切割,查看/var/log/tomcat/oldlog下是否有日志
[root@test01 ~]# /usr/sbin/logrotate -vf /etc/logrotate.d/tomcat
reading config file /etc/logrotate.d/tomcat
olddir is now /var/log/tomcat/oldlog
Allocating hash table for state file, size 15360 B
Handling 1 logs
rotating pattern: /usr/local/tomcat8/logs/catalina.out forced from command line (5000 rotations)
olddir is /var/log/tomcat/oldlog, empty log files are not rotated, old logs are removed
considering log /usr/local/tomcat8/logs/catalina.out
log needs rotating
rotating log /usr/local/tomcat8/logs/catalina.out, log->rotateCount is 5000
Converted ' .%Y%m%d-%H点' -> '.%Y%m%d-%H点'
dateext suffix '.20181226-15点'
glob pattern '.[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9]点'
glob finding old rotated logs failed
fscreate context set to unconfined_u:object_r:usr_t:s0
renaming /usr/local/tomcat8/logs/catalina.out to /var/log/tomcat/oldlog/catalina.out.20181226-15点
compressing log with: /bin/gzip
set default create context to unconfined_u:object_r:usr_t:s0
set default create context
#查看切割结果
#把之前切割的删除了,因为再定时切割文件是相同的,还是catalina.out.20181226-15点.gz这个文件,所以不会切割成功。
[root@test01 ~]# rm -rf /var/log/tomcat/oldlog/catalina.out.20181226-15点.gz
[root@test01 ~]# ls /var/log/tomcat/oldlog/
[root@test01 ~]#
#设置定时任务,每一分钟切割一次(注意:日志里要有内容,因为前面定义空的日志文件是不切割的)
[root@test01 ~]# crontab –e
*/1 * * * * /usr/sbin/logrotate -vf /etc/logrotate.d/tomcat >>/var/log/tomcat/oldlog/cutting.log
#每俩小时切割一次
[root@test02 ~]# crontab -l
0 */2 * * * /usr/sbin/logrotate -vf /etc/logrotate.d/tomcat >> /root/chenjiaxin/cutting.log
#查看定时切割的结果
当然,上面设置一分钟切割一次,是为了实验能尽快看到效果。实际是需要根据公司需要来设置多久来切割一次日志的!
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx