日志切割的方法
温馨提示:这篇文章已超过810天没有更新,请注意相关的内容是否还可用!
之前不懂日志切割,一直觉得很牛逼。 其实这个东西很简单。 如果你仔细阅读这篇文章crontab查看日志,你几乎可以在一天内理解它。 这是一件非常简单的事情。 不要被名字所迷惑。 害怕的!
(图片来源网络,侵删)
(图片来源网络,侵删)
1. linux自带的logrotate日志切割
2、logrotate配置文件分为主配置和子配置
主要配置:/etc/logrotate.conf
子配置:/etc/logrotate.d/下的文件
3、logrotate是基于CRON的crontab查看日志,它的脚本是/etc/cron.daily/logrotate
[root@localhost ~]# cat /etc/cron.daily/logrotate #!/bin/sh /usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf EXITVALUE=$? if [ $EXITVALUE != 0 ]; then /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]" fi exit 0
实际运行时会调用配置文件/etc/logrotate.conf
4.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/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # m h dom mon dow user command 17 * * * * root cd / && run-parts --report /etc/cron.hourly 25 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...]-d, --debug :debug模式,测试配置文件是否有错误。 -f, --force :强制转储文件。 -m, --mail=command :压缩日志后,发送日志到指定邮箱。 -s, --state=statefile :使用指定的状态文件。 -v, --verbose :显示转储过程。
6、logrotate配置文件常用参数
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
七、附上生产环境的几个配置文件
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
}
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

