使用logrotate切割nginx日志文件,其他日志文件切割类似

1 篇文章 0 订阅
0 篇文章 0 订阅

首先安装logrotate

以centos为例

yum -y install logrotate

如果不需要切割其他的日志则把

/etc/logrotate.conf中的

/var/log/wtmp {
    monthly
    create 0664 root utmp
       minsize 1M
    rotate 1
}

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

去掉或者注释掉

删除掉/etc/logrotate.d下的文件,然后新建一个nginx

vi  /etc/logrotate.d/nginx

内容如下

/home/kong/logs/proxy/*.log {

daily

missingok

rotate 90

dateext

#compress

delaycompress

notifempty

create 777 root root

sharedscripts

postrotate

kong restart   #我使用的是kong网关,kong网关重启使用命令kong restart 如果使用的是普通nginx则可使用( [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
                            //重启nginx,重新加载日志文件,防止不写
            `cp -f /var/log/nginx/*.* /var/log/nginx/backup`
                            //自定义脚本,将旧日志copy到backup文件夹(backup要存在))

endscript

}

其中各个配置解释如下:

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             在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
daily                           指定转储周期为每天
weekly                          指定转储周期为每周
monthly                         指定转储周期为每月
rotate count                    指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
tabootext [+] list 让logrotate   不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~ 
size size                       当日志文件到达指定的大小时才转储,bytes(缺省)及KB(sizek)或MB(sizem)

 

查看logrotate默认执行时间

logrotate切割时间默认是在每天的3:22。这个时间点可以通过crontab配置文件查看到。如下:

cat /etc/anacrontab

SHELL=/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

# the maximal random delay added to the base delay of the jobs

RANDOM_DELAY=45

# the jobs will be started during the following hours only

START_HOURS_RANGE=3-22

1 5 cron.daily nice run-parts /etc/cron.daily

7 25 cron.weekly nice run-parts /etc/cron.weekly

@monthly 45 cron.monthly nice run-parts /etc/cron.monthly

其中START_HOURS_RANGE参数就是配置logrotate切割的时间点。

执行logrotate -vf /etc/logrotate.d/nginx 测试

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

懒人烂命

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值