Nginx日志切割工具——logrotate

1、安装

logrotate是Linux系统自带,无需安装

2、配置

进入【/etc/logrotate.d/nginx】文件修改配置

# 需要备份的日志路劲,一个或多个都可以
/data/logs/nginx1/*.log
/data/logs/nginx2/*.log
{
  daily
  rotate 5
  missingok
  dateext
  compress
  notifempty
  sharedscripts
  postrotate
    [ -e /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
  endscript
}

3、测试

logrotate -vf /etc/logrotate.d/nginx 

执行以上命令,是否得到自己预期效果

4、执行

0 0 * * * /usr/sbin/logrotate -vf /etc/logrotate.d/nginx

将上面这段添加到crontab里面,让他每天定时执行

5、参数含义

配置 说明
daily 指定转储周期为每天
weekly 指定转储周期为每周
monthly 指定转储周期为每月
rotate 转储次数,超过将会删除最老的那一个
missingok 忽略错误,如“日志文件无法找到”的错误提示
dateext 切换后的日志文件会附加上一个短横线和YYYYMMDD格式的日期
compress 通过gzip 压缩转储旧的日志
delaycompress 当前转储的日志文件到下一次转储时才压缩
notifempty 如果日志文件为空,不执行切割
sharedscripts 只为整个日志组运行一次的脚本
prerotate/endscript 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
postrotate/endscript 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行

猜你喜欢

转载自www.cnblogs.com/fengchi/p/10124191.html
今日推荐