当前位置: 首页 > linux, 操作系统 > 正文

Logrotate介绍

关键字:
1 星2 星3 星4 星5 星 (2 次投票, 评分: 5.00, 总分: 5)
Loading ... Loading ...
baidu_share

Logrotate是基于CRON来运行的,其脚本是「/etc/cron.daily/logrotate」:

1
2
3
4
5
6
7
8
#!/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

实际运行时,Logrotate会调用配置文件「/etc/logrotate.conf」:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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
 
# 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 -- we'll rotate them here
/var/log/wtmp {
    monthly
    minsize 1M
    create 0664 root utmp
    rotate 1
}
 
# system-specific logs may be also be configured here.

这里的设置可以理解为Logrotate的缺省值,当然了,可以我们在「/etc/logrotate.d」目录里放置自己的配置文件,用来覆盖Logrotate的缺省值。

Logrotate的演示

按天保存一周的Nginx日志压缩文件,配置文件为「/etc/logrotate.d/nginx」:

1
2
3
4
5
6
7
8
9
10
/usr/local/nginx/logs/*.log {
    daily
    dateext
    compress
    rotate 7
    sharedscripts
    postrotate
        kill -USR1 `cat /var/run/nginx.pid`
    endscript
}

如果你等不及CRON,可以通过如下命令来手动执行:

1
shell> logrotate -f /etc/logrotate.d/nginx

当然,正式执行前最好通过Debug选项来验证一下,这对调试也很重要:

1
shell> logrotate -d -f /etc/logrotate.d/nginx

本文固定链接: http://www.chepoo.com/logrotate-introduction.html | IT技术精华网

【上一篇】
【下一篇】

Logrotate介绍:等您坐沙发呢!

发表评论