[root@localhost ~]# vi /etc/cron.daily/logrotate
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf >/dev/null 2>&1
#最主要的就是执行了logrotate命令
EXITVALUE=$?
if [ $EXITVALUE!= 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
[root@localhost ~]# logrotate [选项] 配置文件名
[root@localhost ~]# logrotate -v /etc/logrotate.conf
#查看日志轮替的流程
…省略部分输出…
rotating pattern:/var/log/alert.log weekly (6 rotations)
#这就是我们自己加入轮替的alert.log日志
empty log files are rotated, old logs are removed
considering log /var/log/alert.log
log does not need rotating
#时间不够一周,所以不进行日志轮替
…省略部分输出…
[root@localhost ~]# logrotate -vf /etc/logrotate.conf
#强制进行日志轮替,不管是否符合轮替条件
…省略部分输出…
rotating pattern:/var/log/alert.log forced from command line (6 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/alert.log
log needs rotating
#日志需要轮替
rotating log /var/log/alert.log,log->rotateCount is 6
dateext suffix '-20130607'
#提取日期参数
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
running prerotate script
fscreate context set to unconfined_u:object_r:var_log_t:s0
renaming /var/log/alert.log to /var/log/alert.log-20130607
#旧的日志被重命名
creating new /var/log/alert.log mode = 0600 uid = 0 gid = 0
#创建新日志文件,同时指定权限、所有者和属组
running postrotate script
…省略部分输出…
[root@localhost ~]# ll /var/log/alert.log*
-rw-------.1 root root 0 6月 7 10:07 /var/log/alert.log
-rw-------.1 root root 237 6月 7 09:58 /var/log/alert.log-20130607
#旧的日志文件已经轮替
[root@localhost ~]# lsattr /var/log/alert.log
-----a-------e- /var/log/alert.log
#新的日志文件被自动加入了chattr的a属性
本文链接:http://task.lmcjl.com/news/6798.html