Linux日誌切割logrotate服務配置

1、logrotate介紹

  logrotate軟件是一個日誌管理工具,用於非分隔日誌,刪除舊的日誌文件,並建立新的日誌文件,起到「轉儲做用」,能夠爲系統節省磁盤空間。通常centos系統已經自帶安裝好了。node

  logrotate是基於crontab運行的,其腳本是/etc/cron.daily/logtotate,日誌輪轉是系統自發完成的,實際運行時,logrotate會調用配置文件/etc/logrotate.conf。能夠在/etc/logrotate.d目錄裏放置自定義好的配置文件,用來覆蓋logrotate.conf的缺省值。linux

2、logrotate配置文件

2.1 logrotate默認配置文件nginx

[root@linux-node1 ~]# grep -v '^#' /etc/logrotate.conf weekly #默認每週執行一第二天志輪詢 rotate 4  #默認保留4個日誌文件 create #自動建立新的日誌文件,新的文件和原來的文件具備相同的權限 dateext #日誌切割後,文件以當前日誌爲結尾,例如:messages-20181125 include /etc/logrotate.d #將/etc/logrotate.d目錄中的配置文件加載進來 /var/log/wtmp { #針對wtmp日誌的配置參數 monthly #每個月切割一次 create 0664 root utmp #新建日誌的權限爲0644,屬主爲root,屬組爲utmp minsize 1M #文件大小超過1M後纔會切割 rotate 1  #只保留1個日誌文件 } /var/log/btmp { missingok monthly create 0600 root utmp rotate 1 }

2.2 logrotate經常使用參數centos

配置參數     功能說明 compress    #經過gzip,壓縮轉儲之後的日誌 nocompress   #不須要壓縮時,用這個參數 copytruncate   #用於還在打開中的日誌文件,把當前日誌備份並截斷 nocopytruncate   #備份日誌文件可是不截斷 create mode owner group    #轉儲文件,使用指定的文件模式建立新的日誌文件 nocreate    #不創建新的日誌文件 delaycompress 和 compress   #一塊兒使用時,轉儲的日誌文件到下一次轉儲時才壓縮 nodelaycompress    #覆蓋delaycompress 選項,轉儲同時壓縮。 errors address    #專儲時的錯誤信息發送到指定的Email 地址 ifempty    #即便是空文件也轉儲,這個是 logrotate 的缺省選項。 notifempty   #若是是空文件的話,不轉儲 mail address    #把轉儲的日誌文件發送到指定的E-mail 地址 nomail    #轉儲時不發送日誌文件 olddir directory    #轉儲後的日誌文件放入指定的目錄,必須和當前日誌文件在同一個文件系統 noolddir    #轉儲後的日誌文件和當前日誌文件放在同一個目錄下 prerotate/endscript    #在轉儲之前須要執行的命令能夠放入這個對,這兩個關鍵字必須單獨成行 postrotate/endscript    #在轉儲之後須要執行的命令能夠放入這個對,這兩個關鍵字必須單獨成行 daily    #指定轉儲週期爲天天 weekly    #指定轉儲週期爲每週 monthly    #指定轉儲週期爲每個月 rotate count    #指定日誌文件刪除以前轉儲的次數,0 指沒有備份,5 指保留5 個備份 size(或minsize)   #size當日志文件到達指定的大小時才轉儲,Size 能夠指定 bytes (缺省)以及KB (sizek)或者MB (sizem).

2.3 nginx日誌文件的切割dom

[root@linux-node1 ~]# cat /etc/logrotate.d/nginx /var/log/nginx/*.log { #日誌文件的路徑 daily #天天切割
dateext #日誌切割後,文件以當前日誌爲結尾,例如:access-logs-20181125
missingok #日誌不存在分析,分析下一個 rotate 30 #日誌保留30份 compress #轉存以後壓縮.tar.gz delaycompress #日誌壓縮會被延後到下次分割時進行 notifempty #空文件不轉儲 create 644 nginx adm #新日誌文件權限 sharedscripts #整個日誌組運行一次腳本 postrotate [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid` #重啓nginx,從新加載日誌文件,防止日誌沒法寫入新文件 endscript #結束腳本 }
[root@linux-node1 ~]# logrotate -d /etc/logrotate.d/nginx #進行日誌切割測試
[root@linux-node1 ~]# cat /etc/anacrontab #生效時間是在凌晨3點到22點之間,並且隨機延遲時間是45分鐘
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days   delay in minutes   job-identifier   command
1    5    cron.daily        nice run-parts /etc/cron.daily
7    25    cron.weekly        nice run-parts /etc/cron.weekly
@monthly 45    cron.monthly        nice run-parts /etc/cron.monthly
相關文章
相關標籤/搜索