执行命令
vi /etc/logrotate.d/tomcat
添加内容
#daily 不要加 !! 不然会和linux自带的定时任务重复
//olddir 不会自动创建文件夹,需要手动创建
/usr/software/apache-tomcat-8.5.45/logs/catalina.out {
copytruncate
rotate 365
dateext
nocompress
missingok
ifempty
olddir /usr/software/apache-tomcat-8.5.45/catalina-logs
}
配置完后执行一次测试
/usr/sbin/logrotate -f /etc/logrotate.conf
如果报这个错 在上面的一大堆里面在添加
error: skipping “/usr/local/tomcat8/logs/catalina.out” because parent directory has insecure permissions (It’s world writable or writable by group which is not “root”) Set “su” directive in config file to tell logrotate which user/group should be used for rotation.
su root root
什么是logrotate
logrotate 程序是一个日志文件管理工具。用于分割日志文件,删除旧的日志文件,并创建新的日志文件,起到“转储”作用。可以节省磁盘空间。
logrotate文件位置
该配置文件位于,/etc/logrotate.conf,/etc/logrotate.d/。
还有crontab -e生成的临时文件 在/tmp
常见配置参数
dateext 日期作为后缀
配置linux自带系统定时器
#脚本写到定时计划中去(每天23点59分运⾏)
[root@localhost etc]# crontab -e
59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.d/tomcat
#查看当前定时任务列表
[root@localhost etc]# crontab -l
59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.d/tomcat
重新载入定时配置,启动定时器,查看定时状态
#重启载⼊配置
[root@localhost etc]# systemctl reload crond
#如未启动,则需要启动crontab
[root@localhost etc]# systemctl start cr
[root@localhost etc]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since 二 2021-04-06 16:13:43 CST; 4 months 24 days ago
Process: 1533 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
Main PID: 892 (crond)
Tasks: 1
Memory: 1.0M
CGroup: /system.slice/crond.service
└─892 /usr/sbin/crond -n
[root@localhost etc]# systemctl list-unit-files
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-sys-fs-binfmt_misc.mount static
run-vmblock\x2dfuse.mount disabled
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
tmp.mount disabled
brandbot.path disabled
systemd-ask-password-console.path static
systemd-ask-password-plymouth.path static
systemd-ask-password-wall.path static
session-1029.scope static
session-1049.scope static
session-2783.scope static
session-2784.scope static
session-3581.scope static
arp-ethers.service disabled
auditd.service enabled
autovt@.service enabled
blk-availability.service disabled
brandbot.service static
chrony-dnssrv@.service static
chrony-wait.service disabled
#已启动
chronyd.service enabled
console-getty.service disabled
console-shell.service disabled
container-getty@.service static
........
配置完成后发现每天执行两次 找了半天看日志发现是anacrontab导致的
将配置文件的START_HOURS_RANGE
修改为符合的时间段例如23-24
详细查看:anacrontab
http://c.biancheng.net/view/1095.html
/etc/anacrontab: configuration file for anacron
See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly