$ rpm -qa|grep logrotate
logrotate-3.7.8-17.el6.x86_64
$ rpm -qi logrotate-3.7.8-17.el6.x86_64
Name        : logrotate                    Relocations: (not relocatable)
Version     : 3.7.8                             Vendor: CentOS
Release     : 17.el6                        Build Date: Thu 18 Jul 2013 06:08:08 PM CST
Install Date: Fri 03 Jan 2014 11:27:02 AM CST      Build Host: c6b8.bsys.dev.centos.org
Group       : System Environment/Base       Source RPM: logrotate-3.7.8-17.el6.src.rpm
Size        : 84835                            License: GPL+
Signature   : RSA/SHA1, Thu 18 Jul 2013 06:17:17 PM CST, Key ID 0946fca2c105b9de
Packager    : CentOS BuildSystem <http://bugs.centos.org>
URL         : https://fedorahosted.org/logrotate/
Summary     : Rotates, compresses, removes and mails system log files
Description :
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.  Normally,
logrotate runs as a daily cron job.

Install the logrotate package if you need a utility to deal with the
log files on your system.


logrotate设计用于简化日志文件管理任务。它可以实现自动轮转,压缩,删除日志文件.每个日志文件可以按天,按周或是按月进行管理或者当日志文件增长到一定大小进行管理。

通常情况下,logrotate以每日定时任务的方式运行。它不会再一天之内多次修改日志文件,除非针对这个日志文件设置的logrotate规则是按照日志文件的大小或是增加了-f参数强制日志轮转。


$ 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

相关参数使用:

-d --debug  开启调试模式

-v --verbose 详细输出

-f --force  强制轮转日志文件。可以用于调试新的配置文件

-m --mail <command> 发送邮件时使用什么命令,默认使用/bin/mail -s

-s --state 使用state file


配置文件案例

       # sample logrotate configuration file
       compress

       /var/log/messages {
           rotate 5
           weekly
           postrotate
               /usr/bin/killall -HUP syslogd
           endscript
       }

       "/var/log/httpd/access.log" /var/log/httpd/error.log {
           rotate 5
           mail www@my.org
           size 100k
           sharedscripts
           postrotate
               /usr/bin/killall -HUP httpd
           endscript
       }

       /var/log/news/* {
           monthly
           rotate 2
           olddir /var/log/news/old
           missingok
           postrotate
               kill -HUP ‘cat /var/run/inn.pid‘
           endscript
           nocompress
       }


compress  旧版本的日志文件默认使用gzip压缩

cpmpresscmd  指定使用什么命令来压缩

uncompresscmd 指定使用什么命令来解压缩文件,默认是gunzip

compressext  指定压缩的文件的后缀名称

compressoptions  指定压缩程序的参数。默认使用gzip -9

copy       复制一份日志文件,原有文件保持不变

copytruncate    复制一份日志文件后截断原有日志文件

create mode owner group, create owner group

          轮转旧的日志文件后创建新的日志文件

daily      日志文件每天轮转一次

dateext     使用日期作为轮转后的日志文件的后缀。

dateformat   指定日期格式。只有%Y,%m,%d和%s可以使用。默认是%Y%m%d

delaycompress  推迟压缩

extension    

ifempty     即使是空文件也轮转

include     包含文件或者目录

mail       发送邮件

mailfirst    发送刚刚被压缩的文件

maillast     mail the about-to-expire 文件

maxage     删除几天之前的轮转文件

minsize  size   当日志文件大于size设置的大小时,将日志文件进行轮转。

missingok   如果当一个日志文件消失时,继续轮转下一个日志文件

monthly    通常在一个月的头一天轮转日志文件

nocompress 旧版本的日志文件不压缩

nocopy   不复制原始日志文件

nocopytruncate       不截断原始日志文件

nocreate           不创建新的文件

nodelaycompress

nodateext

nomail

noolddir     轮转的日志文件和原始日志文件放置在同一个目录下

nosharedscripts      对于每一个轮转的日志文件都执行prerotate和postrotate脚本。

notifempty

noshred

olddir directory       日志文件被移动到这个目录下,这个目录必须和日志文件在同一个物理分区上

postrotate/endscript        


weekly            通常是在一周第一天轮转日志

rotate count        在日志文件被删除或者发送邮件出去之前轮转的次数。如果count设为0,日志文件将不被乱转,而是直接被删除。


weekly,daily,monthly都需要看minsize设置的大小,如果还没有超过设定的值将不作轮转



主配置文件/etc/logrotate.conf 

# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
	minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

# system-specific logs may be also be configured here.