logrotate的使用

用途

Centos系統中,logrotate可根據用戶配置的規則檢測和處理日誌文件,配合Cron讓處理定時化。

組成

  • /usr/bin/logrotate 程序位置
  • /etc/cron.daily/logrotate 定時任務執行腳本
  • /etc/logrotate.conf 全局配置文件
  • /etc/logrotate.d應用配置文件存放目錄,覆蓋全局配置

配置說明

  • weekly 每週處理日誌
  • rotate 4 保留4個日誌文件
  • create 創建新的日之間
  • dateext 默認未加時間戳
  • compress 默認不壓縮

Nginx日誌管理

配置文件

/data/wwwlogs/*nginx.log {
        daily
        rotate 30
        missingok
        dateext
        compress
        notifempty
        sharedscripts
        postrotate
            [ -e /var/run/nginx.pid ] && kill -USR1 cat /var/run/nginx.pid
        endscript
    }

定時執行

  • 路徑
    /etc/cron.daily/logrotate
  • shell腳本
#!/bin/sh

/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章