使用logrotate进行日志分割

一、以nginx日志分割为例,在/etc/logrotate.d/下新建对应nginx配置

more /etc/logrotate.d/nginx

/opt/var/log/nginx/*.log {
daily
rotate 3000
dateext
dateformat .%Y-%m-%d
missingok
notifempty
sharedscripts
postrotate
    if [ -f /opt/nginx/logs/nginx.pid ]; then
        kill -USR1 `cat /opt/nginx/logs/nginx.pid`
    fi
endscript
}

具体参数

参 致参数说明
daily日志的轮替周期是毎天
weekly日志的轮替周期是每周
monthly日志的轮控周期是每月
rotate保留的日志文件的个数。0指不留存
compress当进行日志轮替时,对旧的日志进行压缩
create mode owner group建立新日志,同时指定新日志的权限与所有者和所属组.如create 640 root root
mail address当进行日志轮替时.输出内存通过邮件发送到指定的邮件地址
missingok如果日志不存在,则忽略该日志的警告信息
nolifempty如果曰志为空文件,則不进行日志轮替
minsize 大小日志轮替的最小值。也就是日志一定要达到这个最小值才会进行轮持,否则就算时间达到也不进行轮替
size大小日志只有大于指定大小才进行日志轮替,而不是按照时间轮替,如size 100m
dateext使用日期作为日志轮替文件的后缀,如log-20130605
sharedscripts在此关键宇之后的脚本只执行一次
prerotate/cndscript在曰志轮替之前执行脚本命令。endscript标识prerotate脚本结束
postrolaie/endscripl在日志轮替之后执行脚本命令。endscripi标识postrotate脚本结束

二、配置调试(实际不会执行,只是验证配置文件是否有错误)

 /usr/sbin/logrotate -d /etc/logrotate.d/nginx

具体参数
-d, --debug :debug 模式,验证配置文件是否有错误。
-f, --force :强制转储文件。
-m, --mail=command :日志分割后,发送日志到指定邮箱。
-v, --verbose :显示转储过程。

三、添加cron,定时执行(不加也可运行,但logrotate天的默认执行时间,根据系统cron.daily执行,时间不固定)

59 23 * * *  /usr/sbin/logrotate -f /etc/logrotate.d/nginx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值