1、例子:
[root@VM etc]# cat /etc/logrotate.d/tomcat
/opt/apache-tomcat-6.0.26/logs/catalina.out {
daily
dateext
rotate 10
missingok
notifempty
copytruncate
}
2、配置说明:
daily:日志文件每天进行滚动
rotate:保留最5次滚动的日志
notifempty:日志文件为空不进行滚动
dateext 每天执行一次,同时转储旧日志时用日期格式"YYMMDD"代替简单的数字作为标记
sharedscripts:运行postrotate脚本
postrotate :开始脚本
endscript:结束脚本
3.配置生效
/usr/sbin/logrotate /etc/logrotate.conf
4.测试执行
logrotate /etc/logrotate.d/log-file
5.修改执行时间 vi /etc/anacrontab
在 CentOS 是使用 anacron 來設定 logrotate 執行的時段,可修改 /etc/anacrontab 裡面 START_HOURS_RANGE 的設定
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
#改成早上 3~5 點的時候執行
START_HOURS_RANGE=3-5
#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