[root@webdav ~]# vim /etc/logrotate.conf # see "man logrotate"for details # rotate log files weekly weekly # 每周轮替一次,也就是多久生成一个新的日志文件 # keep 4 weeks worth of backlogs rotate 4 # 保留4个轮替日志 # create new (empty) log files after rotating old ones create # 轮替后创建新的日志文件 # use date as a suffix of the rotated file dateext # 使用时间作为轮替文件的后缀 # uncomment this if you want your log files compressed #compress # 如果需要压缩日志,去除注释 # RPM packages drop log rotation information into this directory include /etc/logrotate.d # 让/etc/logrotate.d目录下面配置文件内容参与轮替 # no packages own wtmp and btmp -- we'll rotate them here /var/log/wtmp { # 轮替对象为/var/log/中的wtmp文件 monthly # 每个月轮替一次 create 0664 root utmp #创建新的日志文件的权限,所属用户所属组 minsize 1M # 日志大小大于1M后才能参与轮替 rotate 1 # 保留一个轮替日志文件 }
[root@xor-paasjobworker ~]# cat /etc/anacrontab # /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 # 随机延迟时间是45分钟 # the jobs will be started during the following hours only START_HOURS_RANGE=3-22 # 生效时间范围是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
[root@xor-paasjobadmin ~]# logrotate -d /etc/logrotate.d/nginx reading config file /etc/logrotate.d/nginx Allocating hash table for state file, size 15360 B
Handling 1 logs
rotating pattern: /xor/data2/log/nginx/*log after 1 days (30 rotations) empty log files are not rotated, old logs are removed considering log /xor/data2/log/nginx/4k_access.log log does not need rotating (log has been rotated at 2019-6-20 12:0, that is not day ago yet) considering log /xor/data2/log/nginx/4k_error.log log does not need rotating (log has been rotated at 2019-6-20 12:0, that is not day ago yet) not running postrotate script, since no logs were rotated # 使用‘-d’选项以预演方式运行logrotate。要进行验证,不用实际轮循任何日志文件,可以模拟演练日志轮循并显示其输出。
# 日志转储流程 # crond服务加载/etc/cron.d/0hourly --->在每小时的01分执行/etc/cron.hourly/0anacron脚本 --->执行anacron --->根据/etc/anacrontab的配置执行/etc/cron.daily,/etc/cron.weekly,/etc/cron.monthly --->执行/etc/cron.daily/下的logrotate脚本 --->执行logrotate --->根据/etc/logrotate.conf配置执行脚本/etc/logrotate.d/nginx --->转储nginx日志成功 # 定义每天00点00分转储varnish日志 --------- 方法一 --------- vim /etc/anacrontab # /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 RANDOM_DELAY=0 # 表示延迟为0分钟 # the jobs will be started during the following hours only #START_HOURS_RANGE=3-22 START_HOURS_RANGE=0-22 # 执行从0点到22点 #period in days delay in minutes job-identifier command 1 0 cron.daily nice run-parts /etc/cron.daily # 延迟为0分钟 7 25 cron.weekly nice run-parts /etc/cron.weekly @monthly 45 cron.monthly nice run-parts /etc/cron.monthly # 修改红字部分 # period in days 是表示1天、7天、1个月执行一次 # delay in minutes 是延迟的分钟数 # nice设置优先级为10,范围为-20(最高优先级)到19(最低优先级) # run-parts 是一个脚本,表示会执行它后面目录里的脚本
vim /etc/cron.d/0hourly # Run the hourly jobs SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root 00 * * * * root run-parts /etc/cron.hourly # 修改红字部分,表示每小时的每0分钟开始执行 # 这种方法会影响到 /etc/cron.daily,/etc/cron.weekly,/etc/cron.monthly 下所有脚本的自动执行时间