linux查看日志神器,Linux日志管理神器logrotate使用简介

大家可能都有管理日志的需要,比如定时压缩日志,或者当日志超过一定大小时就自动分裂成两个文件等。最近就接到这样一个小任务。我们的程序用的是C语言,用log4cpp的library来实现日志记录。但是问题是log4cpp并不支持当日志超过一定大小时自动分裂的功能,只能从头覆盖之前的日志,但这显然不是我们想要的。经过一番搜索,我发现其实Linux自带的logrotate命令就能够实现这样的功能。

这是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

下面是一个具体例子:

/var/log/news/news.crit {

monthly

rotate 2

olddir /var/log/news/old

missingok

postrotate

kill -HUP ‘cat /var/run/inn.pid‘

endscript

nocompress

}

monthly:说明是一个月进行一次处理,常用的还有daily,weekly

rotate 2:意思是说最多保留两个备份,多余的老的日志就被覆盖了

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

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

postrotate … endscript:它们以及中间的命令定义了在执行完rotate之后要做什么,一般主要是为了让使用该日志文件的程序知道这个日志被替换了,需要重新打开这个文件

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

logrotate定义了如何处理日志,而它本身则是被

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来保证它会一直开机运行。然后就大工告成了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值