Linux中logrotate的使用

logrotate是linux下日志截断(或轮替)、压缩以及删除工具,如让/var/log/foo日志文件每30天轮替,并删除超过6个月的日志。配置完后,logrotate的运作完全自动化,不必进行任何进一步的人为干预。

主流Linux发行版上都默认安装有logrotate包,如果没有,可以使用apt-get或yum等包管理工具安装。

安装
Debian或Ubuntu

# apt-get install logrotate cron

Fedora、CentOS或RHEL

# yum install logrotate crontabs

配置及使用
logrotate安装完成配置文件是/etc/logrotate.conf,默认会导入所有/etc/logrotate.d/*,所以,一般一类日志会在单独的文件中配置。

下面是一个nginx日志轮替配置:

/var/log/nginx/*log {
    create 0644 nginx nginx
    daily
    rotate 10
    missingok
    notifempty
    compress
    delaycompress
    sharedscripts
    postrotate
        /etc/init.d/nginx reopen_logs
    endscript
}

解释下上面的配置:

/var/log/nginx/*log:glob通配符,告诉logrotate要轮替那些日志文件
create 644 nginx nginx: 以指定的权限创建新的日志文件,同时logrotate也会重命名原始日志文件。
daily: 日志文件将每天轮替。其它可用值为‘daily’,‘weekly’或者‘yearly’。
rotate 10: 除当天的外,保留五天的日志。时间较久的日志将被删除。
missingok: 忽略logrotate运行时,例如“文件无法找到”之类的错误。
notifempty: 如果日志文件为空,不会进行轮替。
compress: 在轮替任务完成后,对日志使用gzip进行压缩
delaycompress: 与compress选项一起用,delaycompress告诉logrotate不要将最近一次的日志归档压缩,压缩将在下一次轮替时进行。
sharedscripts:所有的日志文件都轮替完毕后统一执行一次脚本,而不是每次都执行。
postrotate/endscript: 在所有其它指令完成后,postrotate和endscript里面指定的命令将被执行。上面配置中,告诉nginx重新读取日志。

其他选项
logrotate在轮替日志时,默认会给旧文件加上 .1、 .2、 .3 这样的后缀,.1 是昨天的,.2 是前天的。但如果想要以日期命名,可以在配置文件中加上 dateext,如下:

/var/log/redis/*.log {
weekly
missingok
dateext
copytruncate
rotate 12
compress
notifempty
}
有些软件会在运行时一直打开日志文件,对于这种情况,可以加上copytruncate选项

虽然大多数时候都是按日期切分日志,但同样也会遇到诸如单日日志文件特别小,没几行等情况,这时候可能需要按大小切分,size 可以帮助实现,如下:

/var/www/log/production.log {
        size=1024M
        missingok
        copytruncate #清空原有文件,而不是创建一个新文件
        rotate 12
        compress
        notifempty
}

如上配置就是每1GB切分一次日志。

运行logrotate

logrotate [OPTION...] <configfile>
  -d, --debug               Don't do anything, just test (implies -v)
  -f, --force               Force file rotation
  -m, --mail=command        Command to send mail (instead of `/usr/bin/mail')
  -s, --state=statefile     Path of state file
  -v, --verbose             Display messages during rotation
      --version             Display version information

想要运行logrotate,直接指定配置文件即可,如轮替nginx日志,则可以运行

# logrotate  /etc/logrotate.d/nginx

当配置文件指定为/etc/logrotate.conf,则轮替所有日志:

# logrotate  /etc/logrotate.conf

而大多数情况,都会吧logrotate配置成cron任务,定时运行,而安装的时候也会默认安装cron任务,而将logrotate自身的日志存储在/var/lib/logrotate/status中:

# cat /etc/cron.daily/logrotate
#!/bin/sh

# Clean non existent log file entries from status file
cd /var/lib/logrotate
test -e status || touch status
head -1 status > status.clean
sed 's/"//g' status | while read logfile date
do
    [ -e "$logfile" ] && echo "\"$logfile\" $date"
done >> status.clean
mv status.clean status

test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值