一、logrotate日志切割是linux自带的
二、logrotate配置文件分为主配置和子配置
主配置:/etc/logrotate.conf

子配置:/etc/logrotate.d/下面的文件
三、logrotate是基于CRON运行的,其脚本为/etc/cron.daily/logrotate
[root@localhost ~]# cat /etc/cron.daily/logrotate #!/bin/sh/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.confEXITVALUE=$?if [ $EXITVALUE != 0 ]; then /usr/bin/logger -t logrotate \"大众ALERT exited abnormally with [$EXITVALUE]\公众fiexit 0
实际运行时会调用配置文件/etc/logrotate.conf
四、logrotate基于CRON运行,以是实行韶光由CRON掌握
ubuntu查看/etc/crontab
centos查看/etc/anacrontab
root@qqq:/etc/logrotate.d# cat /etc/crontab # /etc/crontab: system-wide crontab# Unlike any other crontab you don't have to run the `crontab'# command to install the new version when you edit this file# and files in /etc/cron.d. These files also have username fields,# that none of the other crontabs do.SHELL=/bin/shPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin# m h dom mon dow user command17 root cd / && run-parts --report /etc/cron.hourly25 6 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )47 6 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )52 6 1 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )#
没错,run-parts是运行一个目录中的所有脚本或程序,--report的功能与--verbose功能类似,自己去领悟
没错,logrotate脚本是/etc/cron.daily/目录下面的脚本,CRON每天06:25运行/etc/cron.daily/目录下面的所有脚本
五、logrotate命令:
logrotate命令格式:logrotate [OPTION...] <configfile>-d, --debug :debug模式,测试配置文件是否有缺点。-f, --force :逼迫转储文件。-m, --mail=command :压缩日志后,发送日志到指定邮箱。-s, --state=statefile :利用指定的状态文件。-v, --verbose :显示转储过程。
六、logrotate配置文件常见参数
compress 通过gzip 压缩转储往后的日志nocompress 不做gzip压缩处理copytruncate 用于还在打开中的日志文件,把当前日志备份并截断;是先拷贝再清空的办法,拷贝和清空之间有一个韶光差,可能会丢失部分日志数据。nocopytruncate 备份日志文件不过不截断create mode owner group 轮转时指定创建新文件的属性,如create 0777 nobody nobodynocreate 不建立新的日志文件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 100ksize = 100M 或 size 100M
七、附上生产环境的几个配置文件
root@web1:/etc/logrotate.d# cat nginx/var/log/nginx/.log { daily missingok rotate 14 compress delaycompress notifempty create 0640 www-data adm sharedscripts prerotate if [ -d /etc/logrotate.d/httpd-prerotate ]; then \ run-parts /etc/logrotate.d/httpd-prerotate; \ fi \ endscript postrotate invoke-rc.d nginx rotate >/dev/null 2>&1 endscript}
zabbix这个
root@web1:/etc/logrotate.d# cat zabbix-agent /var/log/zabbix/zabbix_agentd.log { weekly rotate 12 compress delaycompress missingok notifempty create 0640 zabbix zabbix}
php这个慢查询和缺点日志
root@web1:/etc/logrotate.d# cat php7.0-fpm /var/log/php7.0-fpm.log { rotate 12 weekly missingok notifempty compress delaycompress postrotate /usr/lib/php/php7.0-fpm-reopenlogs endscript}/var/log/php/.log { rotate 12 daily missingok notifempty compress delaycompress }