logrotate是一個Linux系統默認安裝了的日誌文件管理工具,用來把舊文件輪轉、壓縮、刪除,而且建立新的日誌文件。咱們能夠根據日誌文件的大小、天數等來轉儲,便於對日誌文件管理。html
logrotate是基於crond服務來運行的,其crond服務的腳本是/etc/cron.daily/logrotate,日誌轉儲是系統自動完成的。實際運行時,logrotate會調用配置文件 /etc/logrotate.conf,能夠在 /etc/logrotate.d 目錄裏放置自定義好的配置文件,用來覆蓋logrotate的缺省值。node
/etc/logrotate.conf 是主配置文件
/etc/logrotate.d 是一個目錄,該目錄下的全部文件都會被主動的讀到 /etc/logrotate.conf 中執行python
配置項說明:nginx
配置項 | 說明 |
---|---|
compress | 經過 gzip 壓縮轉儲舊的日誌 |
nocompress | 不須要壓縮時,用這個參數 |
copytruncate | 用於還在打開中的日誌文件,把當前日誌備份並截斷,是先拷貝再清空的方式,拷貝和清空之間有一個時間差,可能會丟失部分日誌數據 |
nocopytruncate | 備份日誌文件可是不截斷 |
create mode owner group | 使用指定的文件模式建立新的日誌文件,如:create 0664 root utmp |
nocreate | 不創建新的日誌文件 |
delaycompress | 和 compress 一塊兒使用時,轉儲的日誌文件到下一次轉儲時才壓縮 |
nodelaycompress | 覆蓋 delaycompress 選項,轉儲同時壓縮 |
missingok | 在日誌轉儲期間,任何錯誤將被忽略 |
errors address | 轉儲時的錯誤信息發送到指定的 Email 地址 |
ifempty | 即便日誌文件是空文件也轉儲,這個是 logrotate 的缺省選項 |
notifempty | 若是日誌文件是空文件的話,不轉儲 |
mail E-mail | 把轉儲的日誌文件發送到指定的 E-mail 地址 |
nomail | 轉儲時不發送日誌文件到 E-mail 地址 |
olddir directory | 轉儲後的日誌文件放入指定的目錄,必須和當前日誌文件在同一個文件系統 |
noolddir | 轉儲後的日誌文件和當前日誌文件放在同一個目錄下 |
prerotate/endscript | 在轉儲以前須要執行的命令能夠放入這個對中,這兩個關鍵字必須單獨成行 |
postrotate/endscript | 在轉儲以後須要執行的命令能夠放入這個對中,這兩個關鍵字必須單獨成行 |
sharedscripts | 全部的日誌文件都轉儲完畢後統一執行一次腳本 |
daily | 指定轉儲週期爲天天 |
weekly | 指定轉儲週期爲每週 |
monthly | 指定轉儲週期爲每個月 |
rotate count | 指定日誌文件刪除以前轉儲的次數,0 指沒有備份,5 指保留5個備份 |
size(minsize) logsize | 當日志文件到達指定的大小時才轉儲,size 能夠指定單位爲k或M,如:size 500k,size 100M |
dateext | 指定轉儲後的日誌文件以當前日期爲格式結尾,如 |
dateformat dateformat | 配合dateext使用,緊跟在下一行出現,定義日期格式,只支持%Y %m %d %s這4個參數,如:dateformat -%Y%m%d%s |
cat > /etc/logrotate.d/nginx << eof
/usr/local/nginx/logs/*log {
daily
# 天天轉儲
rotate 30
# 保存30個備份
missingok
# 在日誌轉儲期間,任何錯誤將被忽略
notifempty
# 文件爲空時不轉儲
compress
# 經過 gzip 壓縮
dateext
# 日誌文件以當前日期爲格式結尾
sharedscripts
# 全部日誌文件轉儲完畢後執行一次腳本
postrotate
# 轉儲以後執行命令,和endscript成對使用
/bin/kill -USR1 \$(cat /usr/local/nginx/logs/nginx.pid 2>/dev/null) 2>/dev/null || :
endscript
# 轉儲以後執行命令,和postrotate成對使用
}
eof
cat > /etc/logrotate.d/syslog << eof
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
sharedscripts
dateext
rotate 25
size 40M
compress
dateformat -%Y%m%d%s
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}
eof
cat > /etc/logrotate.d/redis << eof
/var/log/redis/*.log {
weekly
rotate 10
copytruncate
delaycompress
compress
notifempty
missingok
}
eof
cat > /etc/logrotate.d/supervisor << eof
/var/log/supervisor/*.log {
missingok
weekly
notifempty
nocompress
}
eof
cat > /etc/logrotate.d/yum << eof
/var/log/yum.log {
missingok
notifempty
yearly
create 0600 root root
}
eof
cat > /etc/logrotate.d/httpd << eof
/var/log/httpd/*log {
missingok
notifempty
sharedscripts
postrotate
/sbin/service httpd reload > /dev/null 2>/dev/null || true
endscript
}
eof
cat > /etc/logrotate.d/salt << eof
/var/log/salt/master {
weekly
missingok
rotate 5
compress
notifempty
}
/var/log/salt/minion {
weekly
missingok
rotate 5
compress
notifempty
}
/var/log/salt/key {
weekly
missingok
rotate 5
compress
notifempty
}
/var/log/salt/cloud {
weekly
missingok
rotate 5
compress
notifempty
}
/var/log/salt/ssh {
weekly
missingok
rotate 5
compress
notifempty
}
eof
使用 man logrotate 或者 logrotate --help 來查看相關命令參數redis
logrotate --help
Usage: logrotate [OPTION...] <configfile>
-d, --debug 調試模式,輸出調試結果,並不執行。隱式-v參數
-f, --force 強制轉儲文件
-m, --mail=command 發送郵件命令而不是用‘/bin/mail'發 -s, --state=statefile 狀態文件,對於運行在不一樣用戶狀況下有用 -v, --verbose 顯示轉儲過程的詳細信息
若是等不及cron自動執行日誌轉儲,能夠強制轉儲文件(-f 參數) ,正式執行前最好使用調試模式(-d 參數)shell
/usr/sbin/logrotate -f /etc/logrotate.d/nginx
/usr/sbin/logrotate -d -f /etc/logrotate.d/nginx
如今須要將日誌轉儲(切割)時間調整到天天的0點,即天天切割的日誌是前一天的0點-23點59分59秒之間的內容。vim
首先說一下基於crond服務,logrotate轉儲日誌的流程:運維
crond服務加載/etc/cron.d/0hourly --->在每小時的01分執行/etc/cron.hourly/0anacron --->執行anacron --->根據/etc/anacrontab的配置執行/etc/cron.daily,/etc/cron.weekly,/etc/cron.monthly --->執行/etc/cron.daily/下的logrotate腳本 --->執行logrotate --->根據/etc/logrotate.conf配置執行腳本/etc/logrotate.d/nginx --->轉儲nginx日誌成功dom
重點看一下 /etc/anacrontabssh
# /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 # 隨機的延遲時間,表示最大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
# period in days 是表示1天、7天、1個月執行一次
# delay in minutes 是延遲的分鐘數
# nice設置優先級爲10,範圍爲-20(最高優先級)到19(最低優先級)
# run-parts 是一個腳本,表示會執行它後面目錄裏的腳本
分析能夠得出,若是機器沒有關機,默認的logrotate(配置文件裏設置的是daily)通常會在天天的3點05分到3點45分之間執行。
方法一
經過上面的分析,調整以下:
調整 /etc/anacrontab 配置文件,將 RANDOM_DELAY=0 , START_HOURS_RANGE=0-22 , delay in minutes 改成0。
調整 /etc/cron.d/0hourly 配置文件,將01改成00(/etc/cron.daily那一行)。
這樣logrotate就能夠在天天00點00分轉儲日誌(配置文件裏設置的是daily)
方法二
上面的方法會影響到 /etc/cron.daily,/etc/cron.weekly,/etc/cron.monthly 下全部腳本的自動執行時間,影響範圍有點大,若是咱們僅僅須要nginx的日誌轉儲在天天00點執行怎麼辦?
咱們能夠建立一個文件夾,如 /etc/logrotate.daily.0/ ,放置天天0點須要執行的轉儲日誌配置,而後設置計劃任務,天天00點00分 logrotate -f 強制執行。具體操做以下:
mkdir -p /etc/logrotate.daily.0/
mv /etc/logrotate.d/nginx /etc/logrotate.daily.0/ # 具體配置內容參照上文
crontab -e
#nginx log logrotate
00 00 * * * /usr/sbin/logrotate -f /etc/logrotate.daily.0/nginx >/dev/null 2>&1
注意:/etc/logrotate.d/目錄下相應的配置文件要刪掉,不然天天會自動執行兩次。
運維中的日誌切割操做梳理(Logrotate/python/shell腳本實現)
crontab和anacron和logrotate的關係詳解