用户工具

站点工具


linux:logrotate

这是本文档旧的修订版!


logrotate

logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.

logrotate 基于 crontab 来运行,运行脚本放在 /etc/cron.daily/logrotate 中,每日执行。

配置文件位于 /etc/logrotate.conf,centos 的默认配置如下:

# 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
 
# use date as a suffix of the rotated file
dateext
 
# 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 and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
        minsize 1M
    rotate 1
}
 
/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

Usage

以下以 nginx 的 logrotate 配置文件来说明常用的用法。

/etc/logrotate.d/nginx
/var/log/nginx/*.log {
        daily                      # 每天备份一次
        missingok
        rotate 60                  # 保留60份日志
        compress
        delaycompress              # 除了第一份以外,其余日志都压缩
        notifempty                 # 日志文件为空时,不执行
        create 640 nginx adm       # 创建日志文件的权限
        sharedscripts              # *.log 所有文件执行完毕以后,才执行脚本
        postrotate
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 `cat /var/run/nginx.pid`         # reload nginx
                fi
        endscript
}

Reference

linux/logrotate.1511444762.txt.gz · 最后更改: 2023/12/03 10:24 (外部编辑)