安装logrotate
logrotate是系统默认安装的,如果没有的话,执行下面命令安装
yum -y install logrotate
相关文件
/etc/cron.daily # logrotate脚本文件存放位置
/etc/logrotate.conf # 主配置文件
/usr/sbin/logrotate # 二进制程序文件
/etc/logrotate.d/ # 特定服务日志存储目录(自己设定的配置文件)
/var/lib/logrotate.status # 状态文件
主配置文件默认配置
cat /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly //每周一次日志轮询
# keep 4 weeks worth of backlogs
rotate 4 //保留四个日志备份文件
# create new (empty) log files after rotating old ones
create //rotate后,创建一个新的空文件
# 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 //新日志文件的权限为0644,属主为root,属组为utmp
minsize 1M //日志文件大于1M才会进行切割
rotate 1 //保留一个日志备份,优先于全局设定的四个
}
/var/log/btmp { //指定/var/log/btmp日志文件
missingok //如果日志丢失,不报错
monthly
create 0600 root utmp
rotate 1
}
# system-specific logs may be also be configured here.
logrotate常用参数:
配置参数 功能说明
compress #通过gzip,压缩转储以后的日志,以*.gz结尾
nocompress #不需要压缩时,用这个参数
copytruncate #用于还在打开中的日志文件,把当前日志备份并截断
nocopytruncate #备份日志文件但是不截断
create mode owner group #转储文件,使用指定的文件模式创建新的日志文件,例如:create 644 www root
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 #指定转储周期为每月
dateext # 轮询的文件名字带有日期信息
missingok # 如果日志文件丢失,不要显示错误
tabooext # 不转储指定扩展名的文件,缺省扩展名:cfsaved,.disabled,.dpkg-dist等
sharedscripts # 共享脚本,让postrotate/endscript包含脚本只执行一次即可
dateformat # 配合dateext使用可以为切割后的日志加上YYYYMMDD格式的日期
rotate count #指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
size(或minsize) #size当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)以及KB (sizek)或者MB (sizem).
以上参数都可以在全局主配置文件中定义,或指定为某个日志文件进行配置,注意:使用时参数之间不要冲突。
logrotate日志切割
主配置:/etc/logrotate.conf
子配置:/etc/logrotate.d/下面的文件
在定义日志文件时可以使用通配符,但不建议使用,因为它会包括已切换过的日志,如必须使用,请在号后加上已知的文件扩展名, 例如:.log。
Nginx日志切割
在/etc/logrotate.d/下创建Nginx服务日志配置文件
vim /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 #结束脚本
}
进行日志切割测试
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: /var/log/nginx/*.log after 1 days (30 rotations)
empty log files are not rotated, old logs are removed
No logs found. Rotation not needed.
logrotate命令参数:
-d, --debug //debug模式,测试配置文件是否有错误
-f, --force //强制转储文件
-m, --mail=command //压缩日志后,发送日志到指定邮箱
-s, --state=statefile //使用指定的状态文件
-v, --verbose //显示转储过程
查看logrotate运行状态
cat /var/lib/logrotate/logrotate.status
由于Logrotate是基于cron定时运行的,所以logrotate脚本默认在 /etc/cron.daily/ 目录下,文件名是logrotate。可以设置 /etc/anacrontab 来控制Logrotate何时运行
anacrontab
查看anacrontab默认配置
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 //生效时间是凌晨三点到二十二点之间
#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
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
评论