logrotate日志切割工具

logrotate 是一个日志文件管理工具。用于分割日志,删除旧的日志,并创建新的日志文件,起到日志滚动的作用。

logrotate 是基于 linux 的 CRON 来运行的,其脚本是 /etc/cron.daily/logrotate

logrotate配置文件

默认存放位置:/etc/logrotate.conf


# 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
}

# system-specific logs may be also be configured here.

logrotate.d下各个服务的配置文件,以NGINX为例

存放位置:/etc/logrotate.d

/var/log/nginx/*.log {
        daily
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 nginx adm
        sharedscripts
        postrotate
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 `cat /var/run/nginx.pid`
                fi
        endscript
}

参数详解

daily :指定转储周期为每天
weekly :指定转储周期为每周
monthly :指定转储周期为每月
rotate count :指定日志文件删除之前转储的次数,0 指没有备份,5 指保留 5 个备份
tabooext [+] list:让 logrotate 不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和~
missingok:在日志轮循期间,任何错误将被忽略,例如 “文件无法找到” 之类的错误。
size size:当日志文件到达指定的大小时才转储,bytes (缺省) 及 KB (sizek) 或 MB (sizem)
compress: 通过 gzip 压缩转储以后的日志
nocompress: 不压缩
copytruncate:用于还在打开中的日志文件,把当前日志备份并截断
nocopytruncate: 备份日志文件但是不截断
create mode owner group : 转储文件,使用指定的文件模式创建新的日志文件
nocreate: 不建立新的日志文件
delaycompress: 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress: 覆盖 delaycompress 选项,转储同时压缩。
errors address : 专储时的错误信息发送到指定的 Email 地址
ifempty :即使是空文件也转储,这个是 logrotate 的缺省选项。
notifempty :如果是空文件的话,不转储
mail address : 把转储的日志文件发送到指定的 E-mail 地址
nomail : 转储时不发送日志文件
olddir directory:储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir: 转储后的日志文件和当前日志文件放在同一个目录下
prerotate/endscript: 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行

手动执行logroate切割日志

执行所有服务的日志切割

logrotate /etc/logrotate.conf

执行指定服务的日志切割【nginx为例】

logrotate /etc/logrotate.d/nginx

-d 调试模式执行日志切割,可以模拟演练日志轮循并显示其输出

logrotate -d /etc/logrotate.d/nginx

-f 强制分割日志,即便不满足配置文件要求 -v 显示详细过程

logrotate -vf /etc/logrotate.d/nginx

logrotate 命令参数

logrotate [OPTION...] <configfile>
-d, --debug :debug模式,测试配置文件是否有错误。
-f, --force :强制转储文件。
-m, --mail=command :压缩日志后,发送日志到指定邮箱。
-s, --state=statefile :使用指定的状态文件。
-v, --verbose :显示转储过程。
  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值