logrotate 程序是一个日志文件管理工具,Centos系统基本都默认已安装。它可以用来把旧的日志文件删除,并创建新的日志文件,我们把它叫做“转储”。也可以根据日志文件的大小,也可以根据其天数来转储,这个过程一般通过 cron 程序来执行。logrotate 程序还可以用于压缩日志文件,以及发送日志到指定的E-mail 。
cat /etc/logrotate.conf
# see "man logrotate" for details
# 所有的日志文件每周转储一次
weekly
# 转储的文件保留4份,也就是4周的文件
rotate 4
# 转储日志后,logrotate自动创建新的日志文件
create
# 给转储的日志文件加上日期后缀
dateext
# 压缩日志文件。默认是注释掉的
#compress
# 读入/etc/logrotate.d目录下的日志转储参数,当系统中安装了RPM软件包时,RPM包的日志转储参数一般会自动建立在/etc/logrotate.d目录下
include /etc/logrotate.d
# 对/var/log/wtmp日志转储的配置
/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.
dnsmasq日志分割案例
1. 首先需要开启dnsmasq的日志记录
vi /etc/dnsmasq.conf log-queries log-facility=/var/log/dnsmasq/dnsmasq.log
2. 建立logrotate的配置文件
vi /etc/logrotate.d/dnsmasq
输入:
/var/log/dnsmasq/dnsmasq.log {
notifempty
weekly
dateext
rotate 58
sharedscripts
postrotate
[ ! -f /var/run/dnsmasq.pid ] || kill -USR2 `cat /var/run/dnsmasq.pid`
endscript
}
参数说明:
完整参数参考
3. 执行logrotate
/usr/sbin/logrotate -vf /etc/logrotate.conf logrotate命令格式解释: logrotate [OPTION...] <configfile> -d, --debug :debug模式,测试配置文件是否有错误。 -f, --force :强制转储文件。 -m, --mail=command :发送日志到指定邮箱。 -s, --state=statefile :使用指定的状态文件。 -v, --verbose :显示转储过程。
如果没有报错,生成了转储文件,dnsmasq正常运行,就OK了。
4. cron定时自动执行logrotate
sh /etc/cron.daily/logrotate
vi /etc/crontab 33 3 * * * root run-parts /etc/cron.daily


