linux下日誌自動切換logrotate

安裝

centos下默認已安裝了logrotatenginx

rpm -ql logrotate-3.7.8-12.el6_0.1.x86_64

/etc/cron.daily/logrotate
/etc/logrotate.conf
/etc/logrotate.d
/usr/sbin/logrotate
/usr/share/doc/logrotate-3.7.8
/usr/share/doc/logrotate-3.7.8/CHANGES
/usr/share/doc/logrotate-3.7.8/COPYING
/usr/share/man/man5/logrotate.conf.5.gz
/usr/share/man/man8/logrotate.8.gz
/var/lib/logrotate.status
  • /etc/cron.daily/logrotate 此文件是定時腳本
  • /etc/logrotate.conf 此文件是全局配置文件
  • /etc/logrotate.d 此目錄是存放單獨定製的配置,咱們建立的配置放這裏
  • /usr/sbin/logrotate 二進制應用程序

配置

vi /etc/logrotate.d/mynginx

/var/log/xxx.log {
    daily
    rotate 30
    dateext
    dateformat -%Y%m%d%s
    #compress
    #delaycompress
    missingok
    notifempty
    #create 600 www www
    olddir arch
    #minsize 200M
    sharedscripts
    postrotate
        ps -A | grep 'gunicorn: maste'|cut -f1 -d' ' | xargs kill -HUP
    endscript
}

說明:centos

  • daily 天天切換一次,其餘可用還有weekly,monthly
  • rotate 在刪除最舊的日誌前保留幾份,這裏天天切一次的話保留30天
  • dateext 日誌的後綴,默認是-%Y%m%d,若是沒有此選項,日誌後綴是.1,.2諸如此類
  • dateformat 配合dateext一塊兒使用,可用的參數%Y%m%d%s這4個
  • compress 使用gzip壓縮
  • delaycompress 配合compress一塊兒使用,最新一個切換的日誌不壓縮
  • missingok 切換中遇到日誌不存在忽略錯誤
  • notifempty 空日誌文件不切換
  • create 以哪一個用戶身份權限建立新日誌
  • olddir 將日誌備份到此目錄,相對於日誌的目錄
  • minsize 超過此大小就觸發日誌切換,但不會在知足的時間前切換
  • sizeminsize相似,區別是無論時間的限制,一知足大小就切換
  • sharedscripts 對於prerotate,postrotate 腳本塊而言,好比日誌是使用通配符/var/log/xxx*.log,命令只會執行一次.默認是切一個日誌觸發一次.
  • postrotate/endscript 切換日誌後執行的腳本,基本是讓進程從新讀取配置,由於日誌文件句柄切換並新建後,再也不關聯進程.使用kill -HUP較多,使用程序自帶的也可,好比nginx -s reload.

其餘參數man logrotatepost

測試配置

查看配置有無問題測試

logrotate -d /etc/logrotate.d/mynginx

手動切換一第二天志日誌

logrotate -vf /etc/logrotate.d/mynginx

//ENDcode

相關文章
相關標籤/搜索