1.簡介
Logrotate是基于CRON來運行的,其腳本是「/etc/cron.daily/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
實際運行時,Logrotate會呼叫組態檔「/etc/logrotate.conf」
weekly
rotate 4
create
dateext
compress
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
}
2.日志切割
2.1.Nginx
/usr/local/openresty/nginx/logs/*.log { daily rotate 5 missingok dateext compress notifempty sharedscripts postrotate [ -e /usr/local/openresty/nginx/logs/nginx.pid ] && kill -USR1 `cat /usr/local/openresty/nginx/logs/ngi nx.pid` endscript }
測驗并驗證是否正常
logrotate -f /etc/logrotate.d/nginx
logrotate -d -f /etc/logrotate.d/nginx
2.2.kong
/usr/local/kong/logs/*.log {
daily
rotate 5
missingok
dateext
compress
notifempty
sharedscripts
postrotate
[ -e /usr/local/kong/pids/nginx.pid ] && kill -USR1 `cat /usr/local/kong/pids/nginx.pid`
endscript
}


2.3.java 日志
/usr/local/datax-web/log/*.log {
daily
rotate 7
missingok
dateext
compress
notifempty
copytruncate
}

3.原理說明
3.1.sharedscripts
haredscripts的作用是在所有的日志檔案都輪轉完畢后統一執行一次腳本(匹配*通配符),如果沒有配置這條指令,那么每個日志檔案輪轉完畢后都會執行一次腳本,
3.2.日志時間
Logrotate是基于CRON運行的,所以這個時間是由anacrontab控制的(CentOS 6是crontab),具體可以查詢CRON的組態檔「/etc/crontab」,可以手動改成如23:59等時間
# /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
3.3.功能說明
|
引數 |
說明 |
|---|---|
|
compress |
啟用壓縮,指的是輪替后的舊日志,默認用的是gzip壓縮 |
|
daily |
每天輪替 |
|
dateext |
使用當期日期作為命名格式 |
|
dateformat .%s |
配合dateext使用,緊跟在下一行出現,定義檔案切割后的檔案名,必須配合dateext使用,只支持 %Y %m %d %s 這四個引數 |
|
ifempty |
即使日志檔案是空的也輪替 |
|
|
將輪替后的檔案發送到指定E-mail地址 |
|
copytruncate |
用于還在打開中的日志檔案,把當前日志備份并截斷,是先拷貝再清空的方式,拷貝和清空之間有一個時間差,可能會丟失部分日志資料, |
|
monthly |
一個月輪替一次 |
|
nocompress |
如果在logrotate.conf中啟用了壓縮,這里是做不用壓縮的引數 |
|
nomail |
不發送郵件到任何地址 |
|
notifempty |
如果日志是空的就不輪替(也就是空日志不切割) |
|
olddir + 目錄 |
輪替后日志檔案放入指定的目錄,必須和當前日志檔案在同一個檔案系統 |
|
rotate +次數 |
輪替最多保留之前的資料幾次,超出的將被洗掉或郵件接收,設為0則不保存 |
|
size size |
當日志增長到指定大小的時候開始輪替 |
|
weekly |
如果當前的星期幾比上次輪替的星期幾少,或者過了一個多星期,就會發生輪替通常是在每周的第一天輪替,如果logrotate不是每天運行的,會在第一次有機會時進行輪替 |
|
yearly |
如果當前年份不同于上次輪替的年份,則進行日志輪替 |
|
Missingok |
如果日志丟失,不報錯繼續滾動下一個日志 |
出處:https://waringid.cnblogs.com
如果您覺得閱讀本文對您有幫助,請點擊一下右下方的推薦按鈕,您的推薦將是我寫作的最大動力!
著作權宣告:本文為博主原創或轉載文章,歡迎轉載,但轉載文章之后必須在文章頁面明顯位置注明出處,否則保留追究法律責任的權利,
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/525842.html
標籤:Linux
上一篇:LemonParser-如何為一元減法和減法設定不同的關聯性?
下一篇:如何找到埠的行程號
