• 隐藏侧边栏
  • 展开分类目录
  • 关注微信公众号
  • 我的GitHub
  • QQ:1753970025
Chen Jiehua

Logrotate 

logrotate,一个用来定期处理服务器日志文件的工具,省了不少忙啊 首先,logrotate是基于CRON运行的,脚本位于 /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

Logrotate的配置文件,/etc/logrotate.conf:

# see "man logrotate" for details
# rotate log files weekly
weekly

# use the syslog group by default, since this is the owning group
# of /var/log/syslog.
su root syslog

# 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

# packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp, or btmp -- we'll rotate them here
/var/log/wtmp {
    missingok
    monthly
    create 0664 root utmp
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0660 root utmp
    rotate 1
}

# system-specific logs may be configured here

我们可以看到,其会加载/etc/logrotate.d/目录下的其他配置文件,因此我们可以根据自己的需要编写配置文件. 例如php5-fpm的logrotate配置文件:

/var/log/php5-fpm.log {
    rotate 12
    weekly
    missingok
    notifempty
    compress
    delaycompress
    postrotate
        invoke-rc.d php5-fpm reopen-logs > /dev/null
    endscript
}

最后,我们可以对自己的配置文件进行测试:

logrotate -d -f /etc/logrotate.d/php5-fpm
码字很辛苦,转载请注明来自ChenJiehua《Logrotate》

评论