logrotate 用法

定时清理 nginx日志

vim /etc/crontab    

59 23 * * * root /usr/sbin/logrotate -f /etc/logrotate.d/nginx_access.log >/tmp/logro.log 2>&
59 23 * * * root /usr/sbin/logrotate -f /etc/logrotate.d/nginx_error.log >/tmp/logro.log 2>&

 

vim  /etc/logrotate.d/nginx_access.log

/usr/local/nginx/logs/*.access.log {
daily
rotate 7
missingok
compress
postrotate
/bin/kill -USR1 `/bin/cat /usr/local/nginx/logs/nginx.pid`
endscript
}

 

vim /etc/logrotate.d/hudit-manager

/data/app/hudit/hudit-manager/logs/hudit-manager.log {
      daily
      rotate 12
       compress
      missingok
       notifempty
      copytruncate
      sharedscripts
       postrotate
                cd /data/app/hudit/hudit-manager && sh ./bin/stop.sh && ./bin/start.sh test_oracle
       endscript
}

 

/usr/local/nginx/logs/*.log {
copytruncate
daily
notifempty
rotate 7
missingok
compress
dateext
dateformat .%Y-%m-%d
}

 vim /var/spool/cron/root

30 1 * * * /usr/sbin/logrotate -f /etc/logrotate.d/nginx
40 1 * * * find /usr/local/nginx/logs -mtime +1 -name "access.log.*" -exec rm -f {} \;
40 1 * * * find /usr/local/nginx/logs -mtime +1 -name "error.log.*" -exec rm -f {} \;

 

 

logrotate 程序是一个日志文件管理工具。用来把旧的日志文件删除,并创建新的日志文件,称为日志转储或滚动。可以根据日志文件的大小,也可以根据其天数来转储,这个过程一般通过 cron 程序来执行

配置文件是 /etc/logrotate.conf

  • compress 通过gzip 压缩转储以后的日志
  • nocompress 不需要压缩时,用这个参数
  • copytruncate 用于还在打开中的日志文件,把当前日志备份并截断
  • nocopytruncate 备份日志文件但是不截断
  • create mode owner group 转储文件,使用指定的文件模式创建新的日志文件
  • nocreate 不建立新的日志文件
  • delaycompress 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩
  • nodelaycompress 覆盖 delaycompress 选项,转储并压缩
  • errors address 专储时的错误信息发送到指定的Email 地址
  • ifempty 即使是空文件也转储,是缺省选项。
  • notifempty 如果是空文件的话,不转储
  • mail address 把转储的日志文件发送到指定的E-mail 地址
  • nomail 转储时不发送日志文件
  • olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
  • noolddir 转储后的日志文件和当前日志文件放在同一个目录下
  • prerotate/endscript 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
  • postrotate/endscript 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行
  • daily 指定转储周期为每天
  • weekly 指定转储周期为每周
  • monthly 指定转储周期为每月
  • size 大小 指定日志超过多大时,就执行日志转储
  • rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
  • Missingok 如果日志不存在,提示错误
  • Nomissingok如果日志不存在,继续下一次日志,不提示错误

 

posted @ 2020-12-30 13:21  我才是酷酷的飞  阅读(271)  评论(0编辑  收藏  举报