Linux日志切割logrotate服务配置

一、logrotate介绍

  logrotate软件是一个日志管理工具,用于非分隔日志,删除旧的日志文件,并创建新的日志文件,起到“转储作用”,可以为系统节省磁盘空间。一般centos系统已经自带安装好了。

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

二、logrotate配置文件

2.1 logrotate默认配置文件

 [root@linux-node1 ~]# grep -v '^#' /etc/logrotate.conf 
  weekly                       #默认每周执行一次日志轮询
  rotate 4                     #默认保留4个日志文件
  create                       #自动创建新的日志文件,新的文件和原来的文件具有相同的权限
  dateext                      #日志切割后,文件以当前日志为结尾,例如:messages-20181125
  include /etc/logrotate.d     #将/etc/logrotate.d目录中的配置文件加载进来

  /var/log/wtmp {              #针对wtmp日志的配置参数
      monthly                  #每月切割一次
      create 0664 root utmp    #新建日志的权限为0644,属主为root,属组为utmp
      minsize 1M               #文件大小超过1M后才会切割
      rotate 1                 #只保留1个日志文件
  }

  /var/log/btmp {
      missingok
      monthly
      create 0600 root utmp
      rotate 1
  }

2.2 logrotate常用参数

配置参数                     功能说明
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).

2.3 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

猜你喜欢

转载自www.cnblogs.com/cyleon/p/10095813.html
今日推荐