共計 22910 個字符,預計需要花費 58 分鐘才能閱讀完成。
這篇文章主要講解了“Linux 的 logrotate 是什么”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著丸趣 TV 小編的思路慢慢深入,一起來研究和學習“Linux 的 logrotate 是什么”吧!
登錄到 web 服務上,上來 df -h 一把,果然,掛的一個共享又被撐爆。
root@websrv001 ~ # df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/rootvg-rootvol 12G 5.6G 5.6G 50% / tmpfs 14G 0 14G 0% /dev/shm /dev/sda1 477M 84M 368M 19% /boot /dev/mapper/rootvg-homevol 2.0G 4.0M 1.9G 1% /local/home /dev/mapper/rootvg-tmpvol 1.1G 252M 793M 25% /tmp /dev/mapper/rootvg-varvol 5.8G 1.1G 4.5G 20% /var /dev/mapper/fsvol 298G 298G 0 100% /net/fsvol tmpfs 14G 0 14G 0% /var/tmp
上次是因為 liferay 的 log 太大,這次再看看 liferay 的 log
root@websrv001 ~ # ll -h /data/tomcat/storage/BU001/liferay/logs/ total 199M drwxr-xr-x 2 tomcat tomcat 4.0K Apr 20 13:14 . drwxr-xr-x 5 tomcat tomcat 4.0K Mar 14 2016 .. -rw-r--r-- 1 tomcat tomcat 1.5M Apr 13 23:59 liferay.2020-04-13.log.gzipped_on_2020-04-17.gz -rw-r--r-- 1 tomcat tomcat 2.5M Apr 14 23:59 liferay.2020-04-14.log.gzipped_on_2020-04-18.gz -rw-r--r-- 1 tomcat tomcat 7.8M Apr 15 23:59 liferay.2020-04-15.log.gzipped_on_2020-04-19.gz -rw-r--r-- 1 tomcat tomcat 31M Apr 17 23:58 liferay.2020-04-17.log -rw-r--r-- 1 tomcat tomcat 7.0M Apr 18 23:59 liferay.2020-04-18.log -rw-r--r-- 1 tomcat tomcat 90G Apr 20 17:54 liferay.2020-04-20.log
我列個去,尼瑪,一個 liferay log 文件,能增長到 90G,這也太扯了 吧。果斷刪掉,不知道為什么,每當我要敲 rm -rf 的時候,心里就莫名的緊張。。。
root@websrv001 ~ # rm -rf /data/tomcat/storage/BU001/liferay/logs/liferay.2020-04-20.log
重啟一下服務
root@websrv001 ~ # service tomcat restart
隨后通知了一下開發組那哥們兒,服務器磁盤已經清理,服務已經重啟,那邊也確認了 web 服務又活了,可以用了。隨后,又檢查了一下其它文件夾,結果發現 catalina.out 這個日志文件,又是超級無敵大:89G,我也是醉了。
root@websrv001 ~ # ll -h /data/tomcat/server/BU001/logs/ total 104G drwxrwxr-x 2 root tomcat 12K Apr 20 04:45 . drwxr-xr-x 13 root root 4.0K Apr 16 17:26 .. -rw-r--r-- 1 tomcat tomcat 5.1K Apr 13 10:28 catalina.2020-04-13.log.gz -rw-r--r-- 1 tomcat tomcat 13K Apr 14 20:46 catalina.2020-04-14.log.gz -rw-r--r-- 1 tomcat tomcat 7.1K Apr 16 17:51 catalina.2020-04-16.log.gz -rw-r--r-- 1 tomcat tomcat 89G Apr 20 10:39 catalina.out -rw-r--r-- 1 tomcat tomcat 0 Feb 10 23:57 catalina.out-20200210 -rw-r--r-- 1 tomcat tomcat 352M Apr 13 03:38 catalina.out-20200413
問題雖然解決了,但是這個問題本來就不應該發生??!難道之前做運維的同事都沒有做腳本來監控和限制日志大小嘛?這可是生產系統??!這樣可不行,既然沒人搞,我就來搞搞。于是網上搜了一把,原來 Linux 系統就自帶了一個日志切割神器 mdash; mdash; logrotate
man 了一下 logrotate,看到了以下描述:logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.
Normally, logrotate is run as a daily cron job. It will not modify a log more than once in one day unless the criterion for that log is based on the log s size and logrotate is being run more than once each day, or unless the -f or –force option is used.
原來這個神器是為了方便管理系統大量日志文件而設計的,對日志文件可實現自動 rotation(字面意思是旋轉,覺得理解成日志循環更好一些),壓縮,刪除和通過 email 發送 log 文件。顆粒度控制很好,可以針對每一個 log 文件進行每天,每周,每個月或者當 log 文件過大時進行 rotate。
一般情況下呢,logrotate 是基于每天的 cron job 來執行的,所以對某個 log 文件的 rotae 每天執行一次,除非 rotate 的執行標準是基于 log 文件大小,并且你 logrotate 這個命令每天被執行了多次,也就是你自定義了定時任務,讓 logrotate 每 x 分鐘或者每 x 小時執行一次,再或者你手動執行這個命令添加了 -f/–force 這個選項。
另外,我發現這個神器是系統自帶的,可想而知這個工具是多么的重要,否則 Linux 也不會默認就安裝了這個工具。
那接下來,我們來看看 logrotate 的相關配置以及如何執行 logrotate。
logrotate 的 conf 文件在 /etc/ 下:
[root@labhost ~]# ll /etc/logrotate. logrotate.conf logrotate.d/ [root@labhost ~]# cat /etc/logrotate.conf
那我們來看看 logrotate.conf 文件里面的內容:
[root@labhost ~]# cat /etc/logrotate.conf # see man logrotate for details # rotate log files weekly: 每周 rotate log 文件一次 weekly # keep 4 weeks worth of backlogs:保存最近 4 周的 log 日志,因為上面是每周 rotate 一次 rotate 4 # create new (empty) log files after rotating old ones:rotate 老日志文件之后,創建一個新的空日志文件 create # use date as a suffix of the rotated file:rotate 的文件以日期格式為后綴,比如:access_log-20200422,如果不加這個選項,rotate 的格式為:access_log.1,access_log.2 等等。 dateext # uncomment this if you want your log files compressed:如果想壓縮 rotate 后的文件,把下面 compress 前面的 #號去掉就可以了。 #compress # RPM packages drop log rotation information into this directory:RPM 包的日志 rotation 配置信息,建議放到 /etc/logrotate.d 這個文件夾下,實現自定義控制 log 文件 rotate include /etc/logrotate.d # no packages own wtmp and btmp -- we ll rotate them here:wtmp 和 btmp 這兩個不屬于任何 package,我們就把 rotate 的規則寫到這里 /var/log/wtmp { monthly #每個月執行一次 rotate create 0664 root utmp #創建空文件,權限是 664, 所屬用戶名 所屬用戶組 minsize 1M #日志文件大小超過 1M 才執行 rotate,否則跳過 rotate 1 #rotate 時,只保留一份 rotate 文件 } /var/log/btmp { missingok monthly create 0600 root utmp rotate 1 } # system-specific logs may be also be configured here.# 其它系統日志也可以在這里配置 rotate 規則
關于 rotate 規則的配置,從上面的 logrotate.conf 文件里可以看到,有兩個地方可以配置:
一種是直接在 logrotate.conf 里配置,不過一般適用于非 RPM 包的系統日志文件。
另外一種是如果你要做 rotate 的日志文件是由第三方 RPM 包軟件產生的,需要在 /etc/logrotate.d 這個文件夾下新建一個配置文件,配置相關 rotate 規則。(UnleBen 發現,其實如果你安裝了第三方的軟件包之后,在 /etc/logrotate.d 這個文件夾下就會自動創建了對應軟件的 rotate 配置文件。)
UncleBen 這里準備了一臺干凈的 CentOS7,在安裝 httpd 這個服務之前,我們來看一下 /etc/logrotate.d 這個文件夾下面都有哪些配置文件
[root@labhost ~]# ll /etc/logrotate.d/ total 20 -rw-r--r--. 1 root root 91 Apr 11 2018 bootlog -rw-r--r--. 1 root root 224 Oct 30 2018 syslog -rw-r--r--. 1 root root 100 Oct 31 2018 wpa_supplicant -rw-r--r--. 1 root root 103 Nov 5 2018 yum [root@labhost ~]#
我們現在安裝一下 httpd 服務:
[root@labhost ~]# yum install httpd -y Resolving Dependencies -- Running transaction check --- Package httpd.x86_64 0:2.4.6-90.el7.centos will be installed -- Finished Dependency Resolution ...... Install 1 Package Total download size: 2.7 M Installed size: 9.4 M Downloading packages: httpd-2.4.6-90.el7.centos.x86_64.rpm | 2.7 MB 00:00:04 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : httpd-2.4.6-90.el7.centos.x86_64 1/1 Verifying : httpd-2.4.6-90.el7.centos.x86_64 1/1 Installed: httpd.x86_64 0:2.4.6-90.el7.centos Complete! [root@labhost ~]#
再去 /etc/logrotate.d 這個文件夾下看一下:
[root@labhost ~]# ll /etc/logrotate.d/ total 24 -rw-r--r--. 1 root root 91 Apr 11 2018 bootlog -rw-r--r--. 1 root root 194 Aug 6 2019 httpd -rw-r--r--. 1 root root 224 Oct 30 2018 syslog -rw-r--r--. 1 root root 100 Oct 31 2018 wpa_supplicant -rw-r--r--. 1 root root 103 Nov 5 2018 yum [root@labhost ~]#
可以看到,安裝 httpd 之后,自動創建好了針對 httpd 服務的 log rotate 配置文件,打開看看:
[root@labhost ~]# cat /etc/logrotate.d/httpd /var/log/httpd/*log { missingok notifempty sharedscripts delaycompress postrotate /bin/systemctl reload httpd.service /dev/null 2 /dev/null || true endscript } [root@labhost ~]#
這就是一個標準的,針對第三方軟件的一個 logrotate 配置文件,簡單說明一下:
格式:
log 文件的路徑(支持通配符 *),再加上一對花括號 {}
花括號里面的內容就是 logrotate 的規則參數,需要單獨成行。
參數解讀:
missingok: 如果日志不存在,不產生錯誤信息
notifempty:如果日志文件為空,不做 rotate,這個跟你 ifempty 互斥
sharedscripts:配合 prerotate and postrotate 使用,sharedscripts 開啟的話,如果使用了對需要做 rotate 的 log 文件使用了
通配符,那么 *
prerotate** and postrotate 的腳本只會執行一次。沒有 sharedscripts 的話,每個需要 log 文件做 rotate 的時候都會執行一遍 prerotate and postrotate 的腳本
delaycompress:延遲壓縮舊的日志文件,先 rotate,不進行壓縮,等到下次 rotate 時,才會壓縮上次 rotate 的文件。這個需要跟 compress 一起使用,單獨使用不生效。
postrotate/endscript:rotate 之后想要執行的腳本,需要放在 postrotate 與 endscript 中間,這兩個選項要單獨成行。
除了上面的參數外,還有以下常見參數:
daily 指定 rotate 周期為每天,還有 weekly, monthly。 rotate count 指定 rotate 日志文件保留的數量,如果沒有配置這個參數,就不保留備份,設置 1 的話,就是保 留 1 個 rotate 備份,10 就是保留 10 個 rotate 備份。 size size 當日志文件到達指定的大小時才轉儲,默認的大小單位是 bytes,可以以 k,M,G。比如 size 10k, 10M, 10G。 compress 通過 gzip 壓縮然后備份日志。 nocompress 默認值,不做 gzip 壓縮處理。 delaycompress 和 compress 配合使用,rotate 的日志文件到下一次執行 logrotate 時才進行壓縮處理。 copytruncate 把當前日志備份并截斷,先拷貝原日志文件再清空,由于拷貝和清空之間有一個時間差,可能會丟失部 分日志數據。 create mode owner group rotate 之后,創建新文件的日志文件并指定新文件的屬性,比如: create 644 tomcat tomcat mail address 把 rotate 的日志文件發送到指定的 E -mail。 dateext 使用當期日期作為命名格式,如果指定的 rotate 大于 1,默認 rotate 之后的文件名是:xx.log.1, xx.log.2, xx.log3,如果配置 dateext 規則,那么 rotate 之后的文件名就會以日期結 尾:xx.log.2020-04-20,xx.log.2020-04-21, xx.log.2020-04-22 dateformat -%Y%m%d%H.%s 定義文件 rotate 后的文件名的日期格式,必須配合 dateext 使用,查了一下資料,目前只支持: %Y %m %d %H %s 這幾個個參數(年,月,日,時,秒)
rotate 的規則是配置好了,但是怎么來執行呢?我們來 help 一下。
[root@labhost ~]# logrotate --help Usage: logrotate [OPTION...] configfile -d, --debug Don t do anything, just test (implies -v) -f, --force Force file rotation -m, --mail=command Command to send mail (instead of `/bin/mail) -s, --state=statefile Path of state file -v, --verbose Display messages during rotation -l, --log=STRING Log file --version Display version information Help options: -?, --help Show this help message --usage Display brief usage message [root@labhost ~]#
這里著重說一下 - d 和 - f 兩個選項:
-d:debug 模式,就是先演練一遍,不是真干。
-f:強制模式,實打實的執行。
從上面我們 man logrotate 的信息可以了解到:一般情況下呢,logrotate 是基于每天的 cron job 來執行的,所以對某個 log 文件的 rotae 每天執行一次,除非 rotate 的執行標準是基于 log 文件大小,并且你 logrotate 這個命令每天被執行了多次,也就是你自定義了定時任務,讓 logrotate 每 x 分鐘或者每 x 小時執行一次,再或者你手動執行這個命令添加了 -f/–force 這個選項。
為什么是每天執行一次呢?我們去 /etc/cron.daily/,/etc/cron.weekly/,/etc/cron.monthly/,/etc/cron.hourly/ 可以看到,在 /etc/cron.daily/ 這個文件夾下有一個 logrotate 可執行腳本,那每天就會跑一次啦!
[root@labhost ~]# ll /etc/cron.daily/ total 8 -rwx------. 1 root root 219 Oct 31 2018 logrotate -rwxr-xr-x. 1 root root 618 Oct 30 2018 man-db.cron [root@labhost ~]# ll /etc/cron.weekly/ total 0 [root@labhost ~]# ll /etc/cron.monthly/ total 0 [root@labhost ~]# ll /etc/cron.hourly/ total 4 -rwxr-xr-x. 1 root root 392 Apr 11 2018 0anacron [root@labhost ~]#
那我們來看看 logrotate 腳本里寫的什么內容:
[root@labhost ~]# cat /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 [root@labhost ~]#
簡單理解就是執行 logrotate 命令,并且指定執行 logrotate 時狀態文件和執行的 conf 文件:/etc/logrotate.conf。大家還記不記得在這個文件里,有一行:include /etc/logrotate.d,也就是說在執行 logrotate.conf 這個文件時,一并執行 /etc/logrotate.d 文件夾下的配置文件。下面幾行就是執行出錯時,logger 一下。
所以,我們執行 logrotate 時,可以直接運行:logrotate + confi 文件名
[root@labhost ~]# logrotate /etc/logrotate.d/httpd
如果一切順利,那么 logrotate 命令會執行成功,httpd 的日志文件會被按照你配置的規則進行 rotate。但是如果沒有達到你的期望結果,你就需要來 debug 了。其實推薦大家在放到生產環境之前,最好好是用 - d 選項來測試一下你寫的配置文件是否可以達到你的預期。我們來看一下加上 - d 選項的運行結果:
[root@labhost ~]# logrotate -d /etc/logrotate.d/httpd reading config file /etc/logrotate.d/httpd Allocating hash table for state file, size 15360 B Handling 1 logs rotating pattern: /var/log/httpd/*log 1048576 bytes (no old logs will be kept) empty log files are not rotated, old logs are removed considering log /var/log/httpd/access_log log does not need rotating (log size is below the size threshold) considering log /var/log/httpd/error_log log does not need rotating (log size is below the size threshold) not running postrotate script, since no logs were rotated [root@labhost ~]#
現在我們把 httpd 這個 logrotate 的流程梳理一下:
httpd 這個 logrotate 的配置文件會每天被執行一次,因為 logrotate 腳本默認放置在 /etc/cron.daily/ 下,腳本會根據 /etc/logrotate.conf 配置文件來執行 logrotate 命令,而針對 httpd 服務的 logrotate 配置文件在 /etc/logrotate.d/ 下,此文件夾被包含在了 /etc/logrotate.conf 配置文件里。
如果 /var/log/httpd/ 下沒有以 log 結尾的文件也不會記錄錯誤信息。
如果 /var/log/httpd/ 下的 log 文件是空文件,那么不會執行 rotate 操作。
在執行 logrotate 之后,httpd 服務會被重新加載
對于大多數情況下,每天把日志備份一下已經足夠了。但是像 UncleBen 周一遇到的這種 case:一天之內,log 日志把磁盤給撐爆了。那這種 case,用 logrotate 該如何解決呢?
首先我們來捋一下需求:
要解決 log 文件一天之內增長過快,撐爆磁盤的問題。所以關鍵詞就是:一天之內 - 時間,撐爆磁盤 - 文件大小
執行周期
logrotate 默認是每天,我們的需求是一天之內,所以可以讓 logrotate 每半天(每 12 小時)、每 8 個小時、每 4 個小時、每 2 個小時,甚至是每小時執行一次,都可以。這個看各位心情。
文件大小
超過多大才執行 logrotate,這個也是根據實際環境來確定。UncleBen 遇到這個 case,日志竟然暴漲到 89G,我這里就設置成超過 1G 大小就進行 rotate。
為了能讓各位客官老爺們看到真是的效果,我這里做了一點小小的改動,話不多說了,上 demo:
httpd logrotate 的配置文件更改如下:
version1:在原來基礎上增加了一行:rotate 3
[root@labhost ~]# cat /etc/logrotate.d/httpd /var/log/httpd/*log { rotate 3 missingok notifempty sharedscripts delaycompress postrotate /bin/systemctl reload httpd.service /dev/null 2 /dev/null || true endscript } [root@labhost ~]#
然后我來創建一個 cron job,每分鐘執行一次:
*/1 * * * * /usr/sbin/logrotate /etc/logrotate.d/httpd
然后創建一個監控 httpd 文件夾下 log 文件的腳本:
[root@labhost ~]# cat monitorlog.sh #!/bin/bash while : do sleep 2 ls -hl /var/log/httpd/ done
我們來執行一下 monitorlog.sh,來觀察一下 httpd 的日志文件。注意:我這里是訪問了 Apache 默認的站點,然后手動刷新 Apache 站點,來產生點 access log,然后停止刷新頁面。
[root@labhost ~]# ./monitorlog.sh total 140K -rw-r--r--. 1 root root 79K Apr 24 14:09 access_log -rw-r--r--. 1 root root 12K Apr 24 14:09 error_log total 140K -rw-r--r--. 1 root root 79K Apr 24 14:09 access_log -rw-r--r--. 1 root root 12K Apr 24 14:09 error_log total 96K -rw-r--r--. 1 root root 0 Apr 24 14:10 access_log -rw-r--r--. 1 root root 79K Apr 24 14:09 access_log.1 -rw-r--r--. 1 root root 370 Apr 24 14:10 error_log -rw-r--r--. 1 root root 12K Apr 24 14:10 error_log.1 total 96K -rw-r--r--. 1 root root 0 Apr 24 14:10 access_log -rw-r--r--. 1 root root 79K Apr 24 14:09 access_log.1 -rw-r--r--. 1 root root 370 Apr 24 14:10 error_log -rw-r--r--. 1 root root 12K Apr 24 14:10 error_log.1 ...
我們可以看到:
由于我設定的是每分鐘執行一次 logrotate,并且指定了 httpd 這個 logrotate 配置文件,所以我們配置的規則會每分鐘被執行一次。
/var/log/httpd/ 這個文件夾下:
只有 access_log 和 error_log 這兩個文件。
Apr 24 14:09 時,access_log 文件大小是 79K,error_log 文件大小是 12K。
停止刷新頁面,等待一分鐘之后
/var/log/httpd/ 這個文件夾下:
增加了兩個文件:access_log.1 和 error_log.1
Apr 24 14:10 時,access_log 文件被清空,大小為 0;而 error_log 不是空的,證明一直有 error 信息產生。
為了驗證 notifempty 這個參數是否生效,我們不刷新頁面,再多等兩分鐘,然后從 monitorlog.sh 執行的結果來看:
access_log 沒有被 rotate,因為在第一次 access log 文件被 rotate 之后,沒有訪問日志產生,它的大小為 0,我們在配置文件里加了 notifempty 這個參數,意思就是如果為空,不做 rotate。
Apr 24 14:13 /var/log/httpd/ 文件夾下信息:
total 96K -rw-r--r--. 1 root root 0 Apr 24 14:10 access_log -rw-r--r--. 1 root root 79K Apr 24 14:09 access_log.1 -rw-r--r--. 1 root root 370 Apr 24 14:13 error_log -rw-r--r--. 1 root root 663 Apr 24 14:13 error_log.1 -rw-r--r--. 1 root root 663 Apr 24 14:12 error_log.2 -rw-r--r--. 1 root root 663 Apr 24 14:11 error_log.3
rotate 3 這個參數:
始終保留 3 份 rotate 的文件,那我們多等幾分鐘,再看看 /var/log/httpd/ 這個文件夾下有哪些文件,即可驗證。
rotate 后的文件名:是以. 數字結尾。
Apr 24 14:14 /var/log/httpd/ 文件夾下信息:
total 96K -rw-r--r--. 1 root root 0 Apr 24 14:10 access_log -rw-r--r--. 1 root root 79K Apr 24 14:09 access_log.1 -rw-r--r--. 1 root root 370 Apr 24 14:14 error_log -rw-r--r--. 1 root root 663 Apr 24 14:14 error_log.1 -rw-r--r--. 1 root root 663 Apr 24 14:13 error_log.2 -rw-r--r--. 1 root root 663 Apr 24 14:12 error_log.3
Apr 24 14:15 /var/log/httpd/ 文件夾下信息:
total 96K -rw-r--r--. 1 root root 0 Apr 24 14:10 access_log -rw-r--r--. 1 root root 79K Apr 24 14:09 access_log.1 -rw-r--r--. 1 root root 370 Apr 24 14:15 error_log -rw-r--r--. 1 root root 663 Apr 24 14:15 error_log.1 -rw-r--r--. 1 root root 663 Apr 24 14:14 error_log.2 -rw-r--r--. 1 root root 663 Apr 24 14:13 error_log.3
delaycompress 這個參數:
是必須要和 compress 一起使用的,單獨使用無效,可以看到沒有任何 log 文件被壓縮處理,rotate 前和 rotate 后的文件大小保持一致。
postrotate/endscript 這個參數里的腳本:/bin/systemctl reload httpd.service /dev/null 2 /dev/null || true endscript
就是在 log 日志被 rotate 之后,需要重新 reload httpd service,如果沒有這個腳本會怎么樣呢?我們來到 /etc/logrotate.d/httpd 里,把這 3 行注釋掉。再到 monitorlog.sh 執行的結果來看看:
total 92K -rw-r--r--. 1 root root 0 Apr 24 14:10 access_log -rw-r--r--. 1 root root 79K Apr 24 14:09 access_log.1 -rw-r--r--. 1 root root 370 Apr 24 14:47 error_log.1 -rw-r--r--. 1 root root 663 Apr 24 14:47 error_log.2 -rw-r--r--. 1 root root 663 Apr 24 14:46 error_log.3
注釋掉 postrotate/endscript 腳本后,我們發現:
error_log 文件在執行 rotate 之后,新的 error_log 并沒有被創建出來。
我們來刷新一下頁面,嘗試產生新的 access log,發現可以正常寫入,大小有變化。
total 136K -rw-r--r--. 1 root root 38K Apr 24 14:52 access_log -rw-r--r--. 1 root root 79K Apr 24 14:09 access_log.1 -rw-r--r--. 1 root root 5.4K Apr 24 14:52 error_log.1 -rw-r--r--. 1 root root 663 Apr 24 14:47 error_log.2 -rw-r--r--. 1 root root 663 Apr 24 14:46 error_log.3
但是我們等待文件被 rotate 之后,再來觀察一下,我們發現,access_log 在被 rotate 之后,新的 access_log 文件同樣沒有被創建出來。
total 136K -rw-r--r--. 1 root root 38K Apr 24 14:52 access_log.1 -rw-r--r--. 1 root root 79K Apr 24 14:09 access_log.2 -rw-r--r--. 1 root root 5.4K Apr 24 14:52 error_log.1 -rw-r--r--. 1 root root 663 Apr 24 14:47 error_log.2 -rw-r--r--. 1 root root 663 Apr 24 14:46 error_log.3
如果我們繼續嘗試刷新頁面,發現 access_log.1 和 error_log.1 日志大小都會增長。這個就可以說明 postrotate/endscript 中間腳本的作用了,如果沒有這個腳本,也就是在執行 logrotate 之后,沒有 reload httpd service,那么 httpd 服務會繼續往舊的 log 日志中寫信息,那么針對 /var/log/httpd/*log 后續的 logrotate 命令都不會被執行。為什么?
配置文件里的文件路徑寫了:/var/log/httpd/*log,你是針對以 log 結尾的文件名。
現在被 rotate 之后的文件名全都是以 . 數字結尾的。
total 148K -rw-r--r--. 1 root root 49K Apr 24 15:30 access_log.1 -rw-r--r--. 1 root root 79K Apr 24 14:09 access_log.2 -rw-r--r--. 1 root root 6.9K Apr 24 15:30 error_log.1 -rw-r--r--. 1 root root 663 Apr 24 14:47 error_log.2 -rw-r--r--. 1 root root 663 Apr 24 14:46 error_log.3
記不記得我們還有一個參數叫 copytruncate,我們來試試,在沒有 postrotate/endscript 腳本時,加上這個參數是什么效果。
version2:刪除 postrotate/endscript 腳本,增加 copytruncate
[root@labhost ~]# cat /etc/logrotate.d/httpd /var/log/httpd/*log { rotate 3 missingok notifempty sharedscripts delaycompress copytruncate }
我們先 stop httpd service,然后刪除所有的 access_log 和 error_log,最后 start httpd service,可以看到,httpd 的日志文件正常產生了。
[root@labhost ~]# systemctl stop httpd [root@labhost ~]# rm -rf /var/log/httpd/* [root@labhost ~]# ll /var/log/httpd/ total 0 [root@labhost ~]# [root@labhost ~]# systemctl start httpd [root@labhost ~]# ll /var/log/httpd/ total 4 -rw-r--r--. 1 root root 0 Apr 24 15:47 access_log -rw-r--r--. 1 root root 812 Apr 24 15:47 error_log [root@labhost ~]#
接下來我們再刷新一下頁面,產生一些 access log 和 error log,等待幾分鐘,執行 mornitorlog.sh 來看一下:
access_log 和 error_log 都可以正常 rotate。
新的 access_log 和 error_log 文件都可以被創建出來,并且可以正常寫入 log 日志。
total 60K -rw-r--r--. 1 root root 17K Apr 24 15:51 access_log -rw-r--r--. 1 root root 27K Apr 24 15:51 access_log.1 -rw-r--r--. 1 root root 2.3K Apr 24 15:51 error_log -rw-r--r--. 1 root root 3.6K Apr 24 15:51 error_log.1 -rw-r--r--. 1 root root 812 Apr 24 15:48 error_log.2
所以,比起放在 postrotate/endscript 中間的 reload httpd service 腳本,我更喜歡用 copytruncate 這個參數。
version3:在 version2 的基礎上,增加 dateext 參數
[root@labhost ~]# cat /etc/logrotate.d/httpd /var/log/httpd/*log { rotate 3 missingok notifempty sharedscripts delaycompress copytruncate dateext }
我們嘗試訪問頁面,刷新,產生一些 access log 和 error log。等待幾分鐘,我們執行 monitorlog.sh 來看一下,可以發現:
最近一次被 rotate log 文件時以日期格式結尾的。
我們的 cron job 規定每分鐘執行一次 logrotate,但是在添加 dateext 參數之后失效了。所以:
如果你想要 logrotate 每天執行多次(大于一次),就不要添加 dateext 參數。
如果你既想要 logrotate 每天執行多次(大于一次),還想 rotate 之后的文件以日期格式結尾,有一種方法就是添加 dateformat .%s 參數。
total 144K -rw-r--r--. 1 root root 30K Apr 24 16:55 access_log -rw-r--r--. 1 root root 9.3K Apr 24 15:56 access_log.1 -rw-r--r--. 1 root root 17K Apr 24 15:52 access_log.2 -rw-r--r--. 1 root root 26K Apr 24 16:07 access_log-20200424 -rw-r--r--. 1 root root 27K Apr 24 15:51 access_log.3 -rw-r--r--. 1 root root 4.1K Apr 24 16:55 error_log -rw-r--r--. 1 root root 1.3K Apr 24 15:56 error_log.1 -rw-r--r--. 1 root root 2.3K Apr 24 15:52 error_log.2 -rw-r--r--. 1 root root 3.6K Apr 24 16:07 error_log-20200424 -rw-r--r--. 1 root root 3.6K Apr 24 15:51 error_log.3
version4:在 version3 的基礎上,增加 dateformat -%Y%m%d%H.%s 參數
[root@labhost ~]# cat /etc/logrotate.d/httpd /var/log/httpd/*log { rotate 3 missingok notifempty sharedscripts delaycompress copytruncate dateext dateformat -%Y%m%d%H.%s }
我們再次嘗試訪問頁面,刷新,產生一些 access log 和 error log。等待幾分鐘,我們執行 monitorlog.sh 來看一下,可以發現:
我們的 cron job 現在執行正常了,當 log 文件非空并且有更新時,可以被執行多次。
被 rotate 之后的文件是以日期格式結尾的:
- 橫杠開頭,接著時年月日時,然后句點. 最后是秒,但是最后的.s% 是從 1970 年 1 月 1 日 00:00:00 到目前經歷的秒數。
total 172K -rw-r--r--. 1 root root 7.5K Apr 24 17:41 access_log -rw-r--r--. 1 root root 26K Apr 24 16:07 access_log-20200424 -rw-r--r--. 1 root root 25K Apr 24 17:32 access_log-2020042417.1587720721 -rw-r--r--. 1 root root 14K Apr 24 17:39 access_log-2020042417.1587721141 -rw-r--r--. 1 root root 3.8K Apr 24 17:40 access_log-2020042417.1587721201 -rw-r--r--. 1 root root 1.1K Apr 24 17:41 error_log -rw-r--r--. 1 root root 3.6K Apr 24 16:07 error_log-20200424 -rw-r--r--. 1 root root 3.3K Apr 24 17:32 error_log-2020042417.1587720721 -rw-r--r--. 1 root root 1.8K Apr 24 17:39 error_log-2020042417.1587721141 -rw-r--r--. 1 root root 514 Apr 24 17:40 error_log-2020042417.1587721201
給大家留個問題:如果 dateformat 定義的格式是:-%Y%m%d%H,那么在日志文件非空并且持續更新的情況下,access_log 和 error_log 會多久被 rotate 一次?
version5:在 version4 的基礎上,增加 size 50K 參數
[root@labhost ~]# cat /etc/logrotate.d/httpd /var/log/httpd/*log { size 50K rotate 3 missingok notifempty sharedscripts delaycompress copytruncate dateext dateformat -%Y%m%d%H.%s }
這一次,我們先執行 monitorlog.sh 來看一下 access_log 和 error_log 的大小,再嘗試訪問頁面,刷新,產生一些 access log 和 error log,注意一下,因為我配置的 size 大小是 50K,所以讓 access_log 文件大小 50K,然后等待幾分鐘,觀察 monitorlog.sh 輸出,可以發現:
access_log 和 error_log 文件都沒有被 rotate,因為他們的大小都沒有超過 50K,從下面的結果可以看出:
access_log 大小是 49K
error_log 大小是 6.6K
total 132K -rw-r--r--. 1 root root 49K Apr 24 18:04 access_log -rw-r--r--. 1 root root 26K Apr 24 16:07 access_log-20200424 -rw-r--r--. 1 root root 1.9K Apr 24 17:56 access_log-2020042417.1587722161 -rw-r--r--. 1 root root 3.8K Apr 24 17:58 access_log-2020042417.1587722281 -rw-r--r--. 1 root root 7.5K Apr 24 17:59 access_log-2020042417.1587722341 -rw-r--r--. 1 root root 6.6K Apr 24 18:04 error_log -rw-r--r--. 1 root root 3.6K Apr 24 16:07 error_log-20200424 -rw-r--r--. 1 root root 257 Apr 24 17:56 error_log-2020042417.1587722161 -rw-r--r--. 1 root root 514 Apr 24 17:58 error_log-2020042417.1587722281 -rw-r--r--. 1 root root 1.1K Apr 24 17:59 error_log-2020042417.1587722341
現在我們繼續刷新頁面,讓 access_log 大小 50K,觀察 monitorlog.sh 輸出,可以發現:
Apr 24 18:12 時
logrotate 被執行了一次,由于 access_log 文件大小 50K,所以就被 rotate 了。并保存為:access_log-2020042418.1587723121
由于 error_log 文件大小 50K, 所以此次 logrotate 忽略了這個文件。
total 132K -rw-r--r--. 1 root root 52K Apr 24 18:11 access_log -rw-r--r--. 1 root root 26K Apr 24 16:07 access_log-20200424 -rw-r--r--. 1 root root 1.9K Apr 24 17:56 access_log-2020042417.1587722161 -rw-r--r--. 1 root root 3.8K Apr 24 17:58 access_log-2020042417.1587722281 -rw-r--r--. 1 root root 7.5K Apr 24 17:59 access_log-2020042417.1587722341 -rw-r--r--. 1 root root 7.1K Apr 24 18:11 error_log -rw-r--r--. 1 root root 3.6K Apr 24 16:07 error_log-20200424 -rw-r--r--. 1 root root 257 Apr 24 17:56 error_log-2020042417.1587722161 -rw-r--r--. 1 root root 514 Apr 24 17:58 error_log-2020042417.1587722281 -rw-r--r--. 1 root root 1.1K Apr 24 17:59 error_log-2020042417.1587722341 total 128K -rw-r--r--. 1 root root 0 Apr 24 18:12 access_log -rw-r--r--. 1 root root 26K Apr 24 16:07 access_log-20200424 -rw-r--r--. 1 root root 3.8K Apr 24 17:58 access_log-2020042417.1587722281 -rw-r--r--. 1 root root 7.5K Apr 24 17:59 access_log-2020042417.1587722341 -rw-r--r--. 1 root root 52K Apr 24 18:12 access_log-2020042418.1587723121 -rw-r--r--. 1 root root 7.1K Apr 24 18:11 error_log -rw-r--r--. 1 root root 3.6K Apr 24 16:07 error_log-20200424 -rw-r--r--. 1 root root 257 Apr 24 17:56 error_log-2020042417.1587722161 -rw-r--r--. 1 root root 514 Apr 24 17:58 error_log-2020042417.1587722281 -rw-r--r--. 1 root root 1.1K Apr 24 17:59 error_log-2020042417.1587722341
version6:在 version5 的基礎上,增加 compress 參數
[root@labhost ~]# cat /etc/logrotate.d/httpd /var/log/httpd/*log { compress size 50K rotate 3 missingok notifempty sharedscripts delaycompress copytruncate dateext dateformat -%Y%m%d%H.%s }
我們繼續刷新頁面,讓 access_log 大小 50K,觀察 monitorlog.sh 輸出,可以發現:
Apr 24 17:59 時
logrotate 被執行了一次,由于 access_log 文件大小 50K,所以就被 rotate 了,并且被做了壓縮處理,保存為:access_log-2020042417.1587722341.gz
被壓縮之前大小是 7.5K,被壓縮后是 398,不到 1k,所以強烈推薦添加這個參數,節省空間?。。。?/p>
由于 error_log 文件大小 50K, 所以此次 logrotate 忽略了這個文件。
total 168K -rw-r--r--. 1 root root 0 Apr 24 18:20 access_log -rw-r--r--. 1 root root 26K Apr 24 16:07 access_log-20200424 -rw-r--r--. 1 root root 398 Apr 24 17:59 access_log-2020042417.1587722341.gz -rw-r--r--. 1 root root 1.3K Apr 24 18:12 access_log-2020042418.1587723121.gz -rw-r--r--. 1 root root 84K Apr 24 18:20 access_log-2020042418.1587723601 -rw-r--r--. 1 root root 19K Apr 24 18:19 error_log -rw-r--r--. 1 root root 3.6K Apr 24 16:07 error_log-20200424 -rw-r--r--. 1 root root 257 Apr 24 17:56 error_log-2020042417.1587722161 -rw-r--r--. 1 root root 514 Apr 24 17:58 error_log-2020042417.1587722281 -rw-r--r--. 1 root root 1.1K Apr 24 17:59 error_log-2020042417.1587722341
感謝各位的閱讀,以上就是“Linux 的 logrotate 是什么”的內容了,經過本文的學習后,相信大家對 Linux 的 logrotate 是什么這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是丸趣 TV,丸趣 TV 小編將為大家推送更多相關知識點的文章,歡迎關注!