logrotate日誌切割配置
logrotate軟件是一個日誌管理工具,用於非分隔日誌,刪除舊的日誌文件,並創建新的日誌文件,起到“轉儲作用”,可以為系統節省磁盤空間。一般centos系統已經自帶安裝好了。
logrotate是基於crontab運行的,其腳本是/etc/cron.daily/logtotate,日誌輪轉是系統自發完成的,實際運行時,logrotate會調用配置文件/etc/logrotate.conf。可以在/etc/logrotate.d目錄裏放置自定義好的配置文件,用來覆蓋logrotate.conf的缺省值。
2 logrotate常用配置文件
/etc/logrotate.conf 主配置文件
/etc/logrotate.d/ 該目錄裏的所有文件都會自動被讀入到logrotate.conf文件中
2.1 logrotate.conf
# sed -r '/^$|^#/d' /etc/logrotate.conf
weekly #默認每周執行一次日誌輪詢
rotate 4 #默認保留4個日誌文件
create #自動創建新的日誌文件,新的文件和原來的文件具有相同的權限
dateext #切割後的日誌文件以當前日期為結尾,如xxx.log-2018-2-28
include /etc/logrotate.d #將logrotate.d目錄裏的文件加載進來
/var/log/wtmp { #針對
monthly #每個月切割一次,取代默認的一周
create 0664 root utmp #新建日誌權限為0664,屬主為root,屬組為utmp
minsize 1M #文件大小超過一周後才會切割
rotate 1 #只保留一個日誌
}
/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}
2.2 常用參數
compress 通過gzip 壓縮轉儲以後的日誌
nocompress 不做gzip壓縮處理
copytruncate 用於還在打開中的日誌文件,把當前日誌備份並截斷;是先拷貝再清空的方式,拷貝和清空之間有一個時間差,可能會丟失部分日誌數據。
nocopytruncate 備份日誌文件不過不截斷
create mode owner group 輪轉時指定創建新文件的屬性,如create 0777 nobody nobody
nocreate 不建立新的日誌文件
delaycompress 和compress 一起使用時,轉儲的日誌文件到下一次轉儲時才壓縮
nodelaycompress 覆蓋 delaycompress 選項,轉儲同時壓縮。
missingok 如果日誌丟失,不報錯繼續滾動下一個日誌
errors address 轉儲時的錯誤信息發送到指定的Email 地址
ifempty 即使日誌文件為空文件也做輪轉,這個是logrotate的缺省選項。
notifempty 當日誌文件為空時,不進行輪轉
mail address 把轉儲的日誌文件發送到指定的E-mail 地址
nomail 轉儲時不發送日誌文件
olddir directory 轉儲後的日誌文件放入指定的目錄,必須和當前日誌文件在同一個文件系統
noolddir 轉儲後的日誌文件和當前日誌文件放在同一個目錄下
sharedscripts 運行postrotate腳本,作用是在所有日誌都輪轉後統一執行一次腳本。如果沒有配置這個,那麽每個日誌輪轉後都會執行一次腳本
prerotate 在logrotate轉儲之前需要執行的指令,例如修改文件的屬性等動作;必須獨立成行
postrotate 在logrotate轉儲之後需要執行的指令,例如重新啟動 (kill -HUP) 某個服務!必須獨立成行
daily 指定轉儲周期為每天
weekly 指定轉儲周期為每周
monthly 指定轉儲周期為每月
rotate count 指定日誌文件刪除之前轉儲的次數,0 指沒有備份,5 指保留5 個備份
dateext 使用當期日期作為命名格式
dateformat .%s 配合dateext使用,緊跟在下一行出現,定義文件切割後的文件名,必須配合dateext使用,只支持 %Y %m %d %s 這四個參數
size(或minsize) log-size 當日誌文件到達指定的大小時才轉儲,log-size能指定bytes(缺省)及KB (sizek)或MB(sizem).
當日誌文件 >= log-size 的時候就轉儲。 以下為合法格式:(其他格式的單位大小寫沒有試過)
size = 5 或 size 5 (>= 5 個字節就轉儲)
size = 100k 或 size 100k
size = 100M 或 size 100M
3 nginx日誌切割實例
vim /etc/logrotate.d/nginx #創建nginx日誌切割配置文件
/application/nginx/logs/*.log{
daily
rotate 10
create
dateext
}
# logrotate -d /etc/logrotate.d/nginx 調試測試 -d debug
# logrotate -d /etc/logrotate.d/nginx 手動切割日誌測試
# ls /application/nginx/logs/ 帶日期的表示是切割好的日誌
access.log bbs.log-20180228 error.log www.log
access.log-20180228 blog.log error.log-20180228 www.log-20180228
bbs.log blog.log-20180228 nginx.pid
4 配置好nginx切割日誌生效時間
# cat /etc/anacrontab #此文件裏有生效時間
# /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 #生效時間範圍是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
也就是說,配好的nginx切割日誌,生效時間是在淩晨3點到22點之間,而且隨機延遲時間是45分鐘
logrotate日誌切割配置