學習如何在Linux中使用Logrotate管理各種日誌
Logrotate 旨在管理生成大量日誌記錄的 Ubuntu 系統。它允許自動輪換、壓縮、刪除和郵寄日誌記錄。每個日誌檔案可以每天、每週、每月或在它增長時處理。本文解釋了“學習如何在Linux中使用logrotate管理各種日誌”
要安裝 logrotate,請使用以下命令:
$ sudo apt-get install logrotate
示例輸出應如下所示:
Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: libecap3 squid-common squid-langpack Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: logrotate 0 upgraded, 1 newly installed, 0 to remove and 250 not upgraded. Need to get 37.6 kB of archives. After this operation, 116 kB of additional disk space will be used. Get:1 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 logrotate amd64 3.8.7-2ubuntu2 [37.6 kB] Fetched 37.6 kB in 0s (86.9 kB/s) Selecting previously unselected package logrotate. (Reading database ... 239112 files and directories currently installed.) Preparing to unpack .../logrotate_3.8.7-2ubuntu2_amd64.deb ... Unpacking logrotate (3.8.7-2ubuntu2) ... Processing triggers for man-db (2.7.5-1) ... .................................................................................
要獲取有關 logrotate 的更多選項,請使用以下命令:
$ logrotate --help
示例輸出應如下所示:
Usage: logrotate [OPTION...] -d, --debug Don't do anything, just test (implies -v) -f, --force Force file rotation -m, --mail=command Command to send mail (instead of `/usr/bin/mail') -s, --state=statefile Path of state file -v, --verbose Display messages during rotation --version Display version information Help options: -?, --help Show this help message --usage Display brief usage message
logrotate 的用法應如下所示:
Usage: logrotate [-dfv?] [-d|--debug] [-f|--force] [-m|--mail=command] [-s|--state=statefile] [-v|--verbose] [--version] [-?|--help] [--usage] [OPTION...] <configfile>
示例
要獲取有關 logrotate 檔案的資訊,請使用以下命令:
$ cd /etc/logrotate.d/ /etc/logrotate.d$ ls
示例輸出應如下所示:
apache2 dbconfig-common munin-node speech-dispatcher upstart apport dpkg mysql-server squid yum apt jenkins pm-utils squidguard cacti lightdm ppp ufw cups-daemon munin rsyslog unattended-upgrades
要獲取 jenkins 的配置,請使用以下命令:
$ sudo nano /etc/logrotate.d/jenkins
示例輸出應如下所示:
/var/log/jenkins/jenkins.log {
weekly
copytruncate
missingok
rotate 52
compress
delaycompress
notifempty
size 10k
dateext
maxage 10
compresscmd /bin/bzip2
}如何在 Ubuntu 上安裝 Jenkins
上述結果的總結如下:
weekly – 如果當前星期幾小於上次輪換的星期幾,或者自上次輪換以來已經過去了一個多星期,則日誌記錄將被輪換。
copytruncate – 在建立副本後,就地截斷舊日誌檔案,而不是移動舊日誌檔案並可選地建立一個新檔案。
rotate 52 – 在刪除或郵寄到郵件指令中指定的地址之前,日誌記錄將輪換 52 次。
compress – 預設情況下,舊版本的日誌記錄使用 gzip 壓縮。
delaycompress – 它將先前日誌檔案的壓縮延遲到下一個輪換週期。
notifempty – 如果日誌為空,則不進行輪換。
size 10k – 如果檔案大小等於(或大於)10K,則執行 logrotate。
dateext – 它透過新增日期副檔名(例如 YYYYMMDD)來存檔舊版本的日誌檔案,而不是簡單地新增數字。
maxage 10 – 刪除超過 10 天的已輪換日誌。
compresscmd – 它指定用於壓縮日誌檔案的命令。
Cron 作業
要獲取 logrotate 的每日 cron 作業,請使用以下命令:
$ cat /etc/cron.daily/logrotate
示例輸出應如下所示:
#!/bin/sh # Clean non existent log file entries from status file cd /var/lib/logrotate test -e status || touch status head -1 status > status.clean sed 's/"//g' status | while read logfile date do [ -e "$logfile" ] && echo "\"$logfile\" $date" done >> status.clean mv status.clean status test -x /usr/sbin/logrotate || exit 0 /usr/sbin/logrotate /etc/logrotate.conf
要獲取 logrotate 的狀態,請使用以下命令:
$ cat /var/lib/logrotate/status
示例輸出應如下所示:
logrotate state -- version 2 "/var/log/syslog" 2017-1-23-9:14:34 "/var/log/cacti/cacti.log" 2017-1-23-9:14:34 "/var/log/mail.log" 2017-1-23-9:14:34 "/var/log/kern.log" 2017-1-23-9:14:34 "/var/log/cups/error_log" 2016-12-17-14:5:58 "/var/log/mysql.log" 2017-1-23-9:0:0 "/var/log/ufw.log" 2017-1-23-9:0:0 "/var/log/cacti/rrd.log" 2017-1-9-11:0:0 "/var/log/lightdm/seat0-greeter.log" 2017-1-23-9:14:34 "/var/log/cacti/poller-error.log" 2017-1-9-11:0:0 "/var/log/munin/munin-update.log" 2017-1-17-9:58:3 "/var/log/speech-dispatcher/speech-dispatcher.log" 2017-1-23-9:0:0 "/var/log/debug" 2017-1-23-9:0:0 "/var/log/yum.log" 2017-1-17-9:0:0 "/var/log/munin/munin-node.log" 2017-1-23-9:14:34 ........................................................................
在本文中,我們學習瞭如何在 Linux 中使用 logrotate 管理各種日誌。在我們的下一篇文章中,我們將提供更多基於 Linux 的技巧和提示。敬請關注。
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP