centos-7.6系统默认安装logrotate,默认的配置文件:node
/etc/logrotate.conf
/etc/logrotate.d/
logrotate.conf:为主配置文件
logrotate.d:为配置相关子系统,用于隔离每一个应用配置(Nginx、PHP、Tomcat...)
用于解决个别日志文件过大,不易清理及查看
以haproxy 为例linux
cat /etc/logrotate.d/haproxy /var/log/haproxy/haproxy.log { #文件绝对路径,nginx日志亦同上 daily #指定转储周期为天天 rotate 20 #保留20天的日志 missingok #若是日志文件丢失,不进行显示错误 notifempty #当日志文件为空时,不进行转储 dateext #加上日志格式 compress #经过gzip压缩转储后的日志 sharedscripts #转储完成后运行脚本,postrotate-endscript间为脚本内容,脚本效果为重启rsyslogd服务。 postrotate systemctl restart rsyslog.service endscript }
测试配置文件
logrotate -d /etc/logrotate.d/haproxy
该服务由anacron调用
anacron默认配置以下
修改该服务的调用时间nginx
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 # the jobs will be started during the following hours only #默认为3点到22点随机延迟45分钟执行,修改成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
效果以下centos
-rw------- 1 root root 3030916 Jul 1 03:48 haproxy.log-20190701.gz -rw------- 1 root root 3870655 Jul 2 03:24 haproxy.log-20190702.gz -rw------- 1 root root 4569410 Jul 3 03:39 haproxy.log-20190703.gz -rw------- 1 root root 3715970 Jul 4 03:33 haproxy.log-20190704.gz
Logrotate中其余可配置参数,具体以下:dom
compress //经过gzip 压缩转储之后的日志 nocompress //不作gzip压缩处理 copytruncate //用于还在打开中的日志文件,把当前日志备份并截断;是先拷贝再清空的方式,拷贝和清空之间有一个时间差,可能会丢失部分日志数据。 nocopytruncate //备份日志文件不过不截断 create mode owner group //轮转时指定建立新文件的属性,如create 0777 nobody nobody nocreate //不创建新的日志文件 delaycompress //和compress 一块儿使用时,转储的日志文件到下一次转储时才压缩 nodelaycompress //覆盖 delaycompress 选项,转储同时压缩。 missingok //若是日志丢失,不报错继续滚动下一个日志 errors address //专储时的错误信息发送到指定的Email 地址 ifempty //即便日志文件为空文件也作轮转,这个是logrotate的缺省选项。 notifempty //当日志文件为空时,不进行轮转 mail address //把转储的日志文件发送到指定的E-mail 地址 nomail //转储时不发送日志文件 olddir directory //转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统 noolddir //转储后的日志文件和当前日志文件放在同一个目录下 sharedscripts //运行postrotate脚本,做用是在全部日志都轮转后统一执行一次脚本。若是没有配置这个,那么每一个日志轮转后都会执行一次脚本 prerotate //在logrotate转储以前须要执行的指令,例如修改文件的属性等动做;必须独立成行 postrotate //在logrotate转储以后须要执行的指令,例如从新启动 (kill -HUP) 某个服务!必须独立成行 daily //指定转储周期为天天 weekly //指定转储周期为每周 monthly //指定转储周期为每个月 rotate count //指定日志文件删除以前转储的次数,0 指没有备份,5 指保留5 个备份 dateext //使用当期日期做为命名格式 dateformat .%s //配合dateext使用,紧跟在下一行出现,定义文件切割后的文件名,必须配合dateext使用,只支持 %Y %m %d %s 这四个参数 size(或minsize) log-size //当日志文件到达指定的大小时才转储,log-size能指定bytes(缺省)及KB (sizek)或MB(sizem). 当日志文件 >= log-size 的时候就转储。 如下为合法格式:(其余格式的单位大小写没有试过) size = 5 或 size 5 (>= 5 个字节就转储) size = 100k 或 size 100k size = 100M 或 size 100M