Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2031900
  • 博文数量: 220
  • 博客积分: 8531
  • 博客等级: 中将
  • 技术积分: 4976
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-18 13:33
文章分类

全部博文(220)

文章存档

2017年(1)

2015年(1)

2014年(5)

2013年(6)

2012年(6)

2011年(30)

2010年(37)

2009年(53)

2008年(41)

2007年(40)

分类: LINUX

2009-06-10 14:02:54

    日志轮询是linux中对日志文件的一种处理方式,为防止日志文件过大造成一些应用的问题。

    日志轮询的原理是:按时间或者按文件大小,将日志文件更名,让应用将新的日志写入新的文件中,旧的日志文件可以设置保留一段时间以备检查。

    linux中日志轮询的服务是logrotate,主配置文件是/etc/logrotate.conf和/etc/logrotate.d中的文件。在红旗所有系统中,日志轮询默认是每一周轮转一次日志,保留4个旧日志文件备份。

    如何将某日志文件按照文件大小轮转呢?

    比如:/var/log/httpd/access_log文件,当达到10M时就轮转一次。

    可以通过修改/etc/logrotate.d/httpd配置文件来解决。修改之后如下(其中添加了size=10M,rotate 4):

/var/log/httpd/*log {
    size=10M
    rotate 4
    missingok
    notifempty
    sharedscripts
    postrotate
        /bin/kill -HUP `cat /var/run/httpd.pid 2>/dev/null` 2> /dev/null || true
    endscript
}


    那么测试一下,往/var/log/httpd/access_log中写入信息,直到此文件大小超过10M,但是没有所想像的会生成一个/var/log/httpd/access_log.1文件。为何?

    其实logrotate并非是一个daemon进程,所以logrotate不是时时监控这些日志文件的,而是通过crond计划任务来执行的:


[root@ASIANUX3 logrotate.d]# cat /etc/cron.daily/logrotate
#!/bin/sh

/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
[root@ASIANUX3 logrotate.d]#


可见,是每天的4:02执行logrotate进行各日志文件检查,如果符合条件的,就会进行日志轮转。

为了验证刚才对httpd修改有效,可以手工执行/usr/sbin/logrotate /etc/logrotate.conf,就会看到/var/log/httpd/access_log.1文件生成了,原来/var/log /httpd/access_log大小变成了0。

 

问题:为何说logrotate是每天4:02执行一次呢?


阅读(2776) | 评论(1) | 转发(2) |
给主人留下些什么吧!~~

uingei2009-06-23 18:30:53

/etc/crontab里面 02 4 * * * root run-parts /etc/cron.daily /etc/cron.daily里有logrotate