Logrotate 介紹
logrotate 程序是一個日誌文件管理工具,用於分割日誌文件,刪除舊的日誌文件,並創建新的日誌文件,起到了日誌輪詢的作用,配置方便,簡單好用。
- CentOS7 默認自帶Logrotate,配置文件/etc/logrotate.conf include /etc/logrotate.d
- Logrotate 是基於 cron 來運行的,其腳本在 /etc/cron.daily/logrotate 該腳本由 anacrontab “偵測運行” logrotate.conf ,並將logrotate 日誌記錄在 /var/lib/logrotate/logrotate.status
$ cat /etc/anacrontab # anacron 具體可自行百度,不多做介紹
# /etc/anacrontab: configuration file for anacron
# 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=0-22
1 5 cron.daily nice run-parts /etc/cron.daily
$ cat /etc/cron.daily/logrotate # logrotate 執行腳本
#!/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
$ awk '/^[^#]/' /etc/logrotate.conf
weekly
rotate 4
create
dateext
# 自定義配置 nginx,php,什麼的 在當前文件下面繼續寫就可以 或 配置在logrotate.d目錄下也可以。
include /etc/logrotate.d
/var/log/wtmp {
monthly
create 0664 root utmp
minsize 1M
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}
配置參數詳解
以下爲 { } 中選項介紹
compress 通過gzip 壓縮轉儲以後的日誌
nocompress 不做gzip壓縮處理
delaycompress 和compress 一起使用時,轉儲的日誌文件到下一次轉儲時才壓縮
nodelaycompress 覆蓋 delaycompress 選項,轉儲同時壓縮。
copytruncate 用於還在打開中的日誌文件,把當前日誌備份並截斷;是先拷貝再清空的方式,拷貝和清空之間有一個時間差,可能會丟失部分日誌數據。
nocopytruncate 備份日誌文件不過不截斷
create mode owner group 輪轉時指定創建新文件的屬性,如create 0777 work work
nocreate 不建立新的日誌文件
missingok 如果日誌丟失,不報錯繼續滾動下一個日誌
errors address 轉儲時的錯誤信息發送到指定的Email 地址
ifempty 即使日誌文件爲空文件也做輪轉,這個是logrotate的缺省選項。
notifempty 當日志文件爲空時,不進行輪轉
mail address 把轉儲的日誌文件發送到指定的E-mail 地址
nomail 轉儲時不發送日誌文件
olddir directory 轉儲後的日誌文件放入指定的目錄,必須和當前日誌文件在同一個文件系統
noolddir 轉儲後的日誌文件和當前日誌文件放在同一個目錄下
sharedscripts 運行postrotate腳本,作用是在所有日誌都輪轉後統一執行一次腳本。如果沒有配置這個,那麼每個日誌輪轉後都會執行一次腳本
prerotate 在logrotate轉儲之前需要執行的指令,例如修改文件的屬性等動作;必須獨立成行
postrotate 在logrotate轉儲之後需要執行的指令,例如重新啓動 (kill -HUP) 某個服務!必須獨立成行
endscripts 腳本結束
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).
logrotate 命令介紹
-
Logrotate 測試日誌切割使用 -f 選項,Debug 選項爲 -d
- logrotate -f /etc/logrotate.d/nginx
- logrotate -d /etc/logrotate.d/nginx
命令格式 logrotate [OPTION...] config_file
-d debug模式,測試配置文件是否有錯誤
-f 強制轉儲文件
-s 指定狀態文件
-v 顯示轉儲過程
Nginx 及 PHP 配置示例
- nginx 日誌輪詢切割
$ cat /etc/logrotate.d/nginx
/var/log/nginx/*.log /data/logs/*.com/*log # 這裏是指定日誌在哪裏, * 星號真香 {
daily # 週期每天
missingok # 日誌丟失不報錯,繼續滾動下一個日誌
rotate 15 # 保留 15個備份
compress # 通過gzip 壓縮轉儲以後的日誌
delaycompress # 和上一條配置,上一次的日誌過一天再壓縮
ifempty # 即使日誌爲空,也進行輪轉
create 640 nginx nginx # 新文件的屬性爲 640 屬主屬組爲 nginx
sharedscripts # 所有日誌輪詢後執行一次下面的腳本
postrotate # 指定日誌輪詢後 執行的腳本
if [ -f /run/nginx.pid ]; then
kill -USR1 `cat /run/nginx.pid`
fi
endscript # 腳本結束
}
- php 日誌輪詢切割
/data/server/php/var/log/php-fpm.log {
daily
rotate 15
missingok
notifempty
compress
delaycompress
create 640 nginx nginx
sharedscripts
postrotate
/bin/kill -USR2 `cat /data/server/php/var/run/php-fpm.pid`
endscript
}