Logrotate - Linux log management

問題描述

在公司的 EC2 主機上,我們是運行在 Linux 系統的基礎上,然而服務的日誌檔會不斷的累積,如果不處理的話,會佔用很多空間,所以這邊紀錄一下如何使用 logrotate 來管理日誌檔。當然關於日誌檔的管理,還可以配合 CloudWatch Logs 來做,以後再補充。

解決方法

  1. 安裝 logrotate
    1
    $ sudo yum install logrotate
  2. 建立設定檔
    1
    $ sudo vim /etc/logrotate.d/<file_name>
    1
    2
    3
    4
    5
    6
    7
    8
    9
    /var/log/<file_name> {
    daily
    size 500M
    rotate 7
    compress
    missingok
    notifempty
    create 0644 <user> <group>
    }
    • daily: 每天執行一次
    • size: 檔案大小超過多少時,就執行 (即可不設週期)
    • rotate: 保留幾份日誌檔
    • compress: 壓縮舊的日誌檔
    • missingok: 如果日誌檔不存在,不要報錯
    • notifempty: 如果日誌檔為空,不要報錯
    • create: 建立新的日誌檔,並指定擁有者與群組
  3. 測試
    1
    $ sudo logrotate -d /etc/logrotate.d/<file_name>
  4. 設定 crontab
    1
    $ sudo vim /etc/cron.daily/logrotate
    1
    2
    3
    4
    5
    6
    7
    8
    #!/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
    1
    $ sudo chmod 755 /etc/cron.daily/logrotate
  5. 測試
    1
    $ sudo logrotate -f /etc/logrotate.conf
  6. 查看 logrotate 狀態
    1
    $ sudo cat /var/lib/logrotate/logrotate.status
  7. 查看 logrotate.conf 設定檔
    1
    $ sudo cat /etc/logrotate.conf
  8. 查看 logrotate.d 服務設定檔
    1
    $ sudo cat /etc/logrotate.d/<file_name>
  9. 查看 crontab 的運行週期
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    sudo cat /etc/anacrontab

    # /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
    以上就會是你的 crontab 日週月的運行時間,如果 logrotate.d 中的各設定檔有指定 size 參數,則可以不必設定週期,會依照日誌大小做切割管理。

關於 Daily 執行的小問題

由於使用 daily 的設定來執行 logrotate 的話,他其實是在一個執行時間範圍內去執行,無法指定執行的時間,所以我們可以考慮自己設定一個 cron job 來執行,以下是設定檔的範例:

  1. 將 /etc/logrotate.d/<設定檔>搬到一個新的目錄,例如 /etc/logrotate.d/daily
    1
    2
    $ sudo mkdir -p /etc/logrotate.d/daily
    $ sudo mv /etc/logrotate.d/<設定檔> /etc/logrotate.d/daily/.
  2. 建立一個新的 cron job
    1
    2
    3
    $ sudo crontab -e
    # 每天 23:59 執行 logrotate
    59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.d/daily/<設定檔> > /dev/null 2>&1
    這樣應該就可以指定時間去執行 logrotate 了。那麼原先的 daily 是在什麼時候執行呢?以 cron.daily 為例,是在每天的 03:05 執行,但因為 RANDOM_DELAY 的關係,所以實際上會在 03:05 ~ 03:50 之間的某個時間執行。又因為 START_HOURS_RANGE 的關係,所以實際上會在 03:05 ~ 22:00 之間的某個時間執行。有這樣的不確定性存在。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    $ sudo cat /etc/anacrontab
    # 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

參考資料

Linux 日誌切割神器 logrotate 原理介紹和配置詳解
linux logrotate 精確時間點執行切割檔案
logrotate自定义切割时间的一些坑