用Linux自带的Logrotate来管理日志

Logrotate是由cron控制,cron在规定的时间执行 " logrotate  /etc/logrotate.conf "命令。将对象日志进行转储,删除,压缩等操作。。。

这是logrotate日志轮替工具的一段官方简介:

The logrotate utility is designed to simplify the administration of log files on a system which generates a lot of log files. Logrotate allows for the automatic rotation compression, removal and mailing of log files. Logrotate can be set to handle a log file daily, weekly, monthly or when the log file gets to a certain size.

为了使用它,主要有两个地方需要修改一下:一个是/etc/logrotate.conf,另一个是/etc/logrotate.d/下面的文件。

你既可以在logrotate.conf中直接定义如何处理你的log文件,也可以在/logrotate.d/下面针对自己的log新建一个对应的文件来定义处理log的行为。

这里是logrotate命令的详细解释的链接:http://linuxcommand.org/man_pages/logrotate8.html

 

下面是一个对Nginx日志进行定时切割压缩的例子:(Tomcat下的catalina.out日志同理

# cat  /etc/logrotate.d/nginx 

/data/logs/nginx/*.log {
    rotate 365
    copytruncate
    create
    missingok
    notifempty
daily sharedscripts compress postrotate [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid` endscript }

monthly:说明是一个月进行一次处理,常用的还有daily,weekly。加上daily之后,日志格式会变成 *.log-20170313.gz

rotate 365:意思是说轮替时最多保留365个备份,多余的老的日志就被覆盖了。最新生成的始终为 *.1.gz ,依次往后推和进行覆盖

copytruncate: 的作用在于先复制一份当前日志文件用做处理,再清空源日志文件,让其继续接收日志。(清空但不删除日志文件)

create:处理完该日志文件后,新生成一个日志文件,当然尽可能是同名同权限等

olddir:定义了旧的日志存储在哪里

missingok:意思是如果上述*.log文件找不到的话也不报错,直接跳过

notifempty: 如果日志文件为空的话,则不进行rotate轮替操作

sharedscripts:和endscript对应,中间放脚本

prerotate: 在启动logrotate之前执行的命令或脚本,例如修改文件或目录的属性等

postrotate:在启动logrotate之后执行的命令或脚本,例如使某项服务重新载入配置文件,重新打开日志文件 (kill -HUP或 kill -USR1)

这里多说一句,kill -HUP与kill -USR1的区别仅在于kill -HUP会对进程进行复位操作,相当于/usr/local/nginx/sbin/nginx -s reload,而kill -USR1执行后主子进程号均不变

nocompress:说明旧日志不需要被压缩保存

minsize 1G: 日志最小1G,不到1G不轮替

 

logrotate定义了如何处理日志,而它本身则是被crond定时调用的。crond是一个Unix系操作系统中的定时调度软件,下面一段文字是从wiki上抄来的:

The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration—though its general-purpose nature makes it useful for things like connecting to the Internet and downloading email at regular intervals.

默认的logrotate是一天运行一次,它的脚本被放在/etc/cron.daily/下面。除了cron.daily外还有cron.weekly,cron.monthly,cron.hourly等分别对应不同的频率,你可以根据自己的需要把脚本放在不同的文件夹下面。在设置外所有东西以后,别忘了使用chkconfig crond on来保证它会一直开机运行。然后就大工告成了。

# cat /etc/cron.daily/logrotate (默认的) 

#!/bin/sh

/usr/sbin/logrotate /etc/logrotate.conf >/dev/null 2>&1
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

 

如果想自定义执行时间的频率的话,自定义计划任务即可,例如下面:(每个整点执行一次)

0 * * * *  root  /usr/sbin/logrotate  -f  /root/nginx              -f, --force        Force file rotation

 

查看各log文件rotate的具体执行情况:

cat  /var/lib/logrotate.status

 
 
 
参考资料:http://www.cnblogs.com/tiantiandas/p/rsyslog.html
              http://www.cnblogs.com/futeng/p/4785206.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值