nginx日志切割


使用系統自帶的logrorate來切個nginx日志,位於/usr/sbin/logrotate

[root@PayServer haproxy]# cat /etc/logrotate.d/nginx 
/home/nginx/logs/*.log {  #指定切割日志文件路徑
        daily
        missingok
        dateext
        rotate 365
        nocompress
        notifempty
        olddir /home/nginx/logs/days/  #指定切割后日志存放的位置
        create 755 root adm
        sharedscripts
        postrotate
                [ -f /home/nginx/logs/nginx.pid ] && kill -USR1 `cat /home/nginx/logs/nginx.pid`
        endscript
}

  • 需要注意的是你們的nginx.pid位置,不一定是在/home/nginx/logs/nginx.pid
 

 

配置 說明
daily 指定轉儲周期為每天
weekly 指定轉儲周期為每周
monthly 指定轉儲周期為每月
rotate 轉儲次數,超過將會刪除最老的那一個
missingok 忽略錯誤,如“日志文件無法找到”的錯誤提示
dateext 切換后的日志文件會附加上一個短橫線和YYYYMMDD格式的日期
compress 通過gzip 壓縮轉儲舊的日志
delaycompress 當前轉儲的日志文件到下一次轉儲時才壓縮
notifempty 如果日志文件為空,不執行切割
sharedscripts 只為整個日志組運行一次的腳本
prerotate/endscript 在轉儲以前需要執行的命令可以放入這個對,這兩個關鍵字必須單獨成行
postrotate/endscript 在轉儲以后需要執行的命令可以放入這個對,這兩個關鍵字必須單獨成行

執行該命令測試

logrotate -vf /etc/logrotate.d/nginx

reading config file /etc/logrotate.d/nginx
reading config info for /home/nginx/logs/*.log 
olddir is now /home/nginx/logs/days/

Handling 1 logs

rotating pattern: /home/nginx/logs/*.log  forced from command line (365 rotations)
olddir is /home/nginx/logs/days/, empty log files are not rotated, old logs are removed
considering log /home/nginx/logs/access.log
  log does not need rotating
considering log /home/nginx/logs/error.log
  log needs rotating
rotating log /home/nginx/logs/error.log, log->rotateCount is 365
dateext suffix '-20180806'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
renaming /home/nginx/logs/error.log to /home/nginx/logs/days//error.log-20180806
creating new /home/nginx/logs/error.log mode = 0755 uid = 0 gid = 4
running postrotate script

查看生產的日志文件

 

每日0點執行腳本

  • 在終端運行 crontab -e
  • 插入以下語句
0 0 * * * /usr/sbin/logrotate -vf /etc/logrotate.d/nginx

參考鏈接:http://www.cnblogs.com/snowater/p/8340238.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM