ops-12 Linux logrotate 详解

logrotate 配置文件参数详解


    logrotate是基于crontab运行的,其脚本是/etc/cron.daily/logtotate,日志轮转是系统自发完成的,实际运行时,logrotate会调用配置文件/etc/logrotate.conf。可以在/etc/logrotate.d目录里放置自定义好的配置文件,用来覆盖logrotate.conf的缺省值。

## 配置参数                     功能说明
compress                    #通过gzip,压缩转储以后的日志
nocompress                   #不需要压缩时,用这个参数
copytruncate                 #用于还在打开中的日志文件,把当前日志备份并截断
nocopytruncate               #备份日志文件但是不截断
create mode owner group     #转储文件,使用指定的文件模式创建新的日志文件
nocreate                    #不建立新的日志文件
delaycompress 和 compress   #一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress             #覆盖delaycompress 选项,转储同时压缩。
errors address              #专储时的错误信息发送到指定的Email 地址
ifempty                     #即使是空文件也转储,这个是 logrotate 的缺省选项。
notifempty                  #如果是空文件的话,不转储
mail address                #把转储的日志文件发送到指定的E-mail 地址
nomail                      #转储时不发送日志文件
olddir directory            #转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir                    #转储后的日志文件和当前日志文件放在同一个目录下
prerotate/endscript         #在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
postrotate/endscript        #在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行
daily                       #指定转储周期为每天
weekly                      #指定转储周期为每周
monthly                     #指定转储周期为每月
rotate count                #指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
size(或minsize)             #size当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)以及KB (sizek)或者MB (sizem).

nginx 日志切割 举例


[root@linux-node1 ~]# cat /etc/logrotate.d/nginx 
/var/log/nginx/*.log {     #日志文件的路径
      daily                #每天切割
      dateext              #日志切割后,文件以当前日志为结尾,例如:access-logs-20181125
      missingok            #日志不存在分析,分析下一个
      rotate 30            #日志保留30份 
      compress             #转存之后压缩.tar.gz
      delaycompress        #日志压缩会被延后到下次分割时进行
      notifempty           #空文件不转储
      create 644 nginx adm #新日志文件权限
      sharedscripts        #整个日志组运行一次脚本
      postrotate
              [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`         #重启nginx,重新加载日志文件,防止日志无法写入新文件
      endscript            #结束脚本
}
[root@linux-node1 ~]# logrotate -d /etc/logrotate.d/nginx         #进行日志切割测试
[root@linux-node1 ~]# cat /etc/anacrontab                         #生效时间是在凌晨3点到22点之间,而且随机延迟时间是45分钟
# /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
posted @ 2021-06-28 14:24  冰冷的火  阅读(128)  评论(0)    收藏  举报