四、使用include 选项覆盖缺省设置

 /etc/logrotate.conf 读入文件时,include 指定的文件中的转储参数将覆盖缺省的参数,如下例:
# linuxconf 的参数
/var/log/htmlaccess.log
{ errors jim
notifempty
nocompress
weekly
prerotate
/usr/bin/chattr -a /var/log/htmlaccess.log
endscript
postrotate
/usr/bin/chattr +a /var/log/htmlaccess.log
endscript
}
/var/log/netconf.log
{ nocompress
monthly
}
在这个例子中,当 /etc/logrotate.d/linuxconf 文件被读入时,下面的参数将覆盖/etc/logrotate.conf中缺省的参数。
Notifempty
errors jim
五、为指定的文件设置转储参数
经常需要为指定文件设置参数,一个常见的例子就是每月转储/var/log/wtmp。为特定文件而使用的参数格式是:
注释
/full/path/to/file
{
option(s)
}
下面的例子就是每月转储 /var/log/wtmp 一次:
#Use logrotate to rotate wtmp
/var/log/wtmp
{
monthly
rotate 1
}
六、其他需要注意的问题
1、尽管花括号的开头能和其他文本放在同一行上,不过结尾的花括号必须独立成行。
2、使用 prerotate  postrotate 选项
下面的例子是典型的脚本 /etc/logrotate.d/syslog,这个脚本只是对
/var/log/messages 有效。
/var/log/messages

prerotate
/usr/bin/chattr -a /var/log/messages
endscript
postrotate
/usr/bin/kill -HUP syslogd
/usr/bin/chattr +a /var/log/messages
endscript
}
第一行指定脚本对 /var/log messages 有效
花括号外的/var/log messages 

prerotate 命令指定转储以前的动作/usr/bin/chattr -a 去掉/var/log/messages文件的只追加属性 endscript 结束 prerotate 部分的脚本postrotate 指定转储后的动作
/usr/bin/killall -HUP syslogd 
用来重新初始化系统日志守护程式 syslogd
/usr/bin/chattr +a /var/log/messages 
重新为 /var/log/messages 文件指定只追加属性,这样防治程式员或用户覆盖此文件。
最后的 endscript 用于结束 postrotate 部分的脚本
3logrotate 的运行分为三步:
判断系统的日志文件,建立转储计划及参数,通过cron daemon 运行下面的代码是 Red Hat Linux 缺省的crontab 来每天运行logrotate
#/etc/cron.daily/logrotate
#! /bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
4/var/log/messages 不能产生的原因:
这种情况非常少见,不过如果你把/etc/services 中的 514/UDP 端口关掉的话,这个文件就不能产生了。
小结:本文通过对Red Hat 系统上典型的logrotate 设置例子的介绍,周详说明了logrotate 程式的应用方法。希望对所有Linux 系统管理员有所帮助。管理好,分析好日志文件是系统安全的第一步,在以后的文章里FreeLAMP还会介绍另外一个检查日志的好东东 logcheck

5logrotate f filename

强制执行日志分割

我们可以使用以下命令,强制执行一次,以后就会按定时计划执行

logrotate -f /etc/logrotate.d/httpd