Apache-2.2

Cent OS logrotate 不旋轉 httpd 日誌

  • March 30, 2015

我的 /etc/logrotate.d/httpd文件內容是

/var/log/httpd/access.log  {
size=50M
dateext
maxage 90
postrotate
/usr/bin/killall -HUP httpd
     ls -ltr /var/log/httpd/ | mail -s "$HOSTNAME: Apache restarted and log files rotated" info@email.com
endscript
}

而我的/etc/logrotate.conf

# see "man logrotate" for details
# rotate log files weekly
weekly 

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
   monthly
   create 0664 root utmp
   minsize 1M
   rotate 1
}

/var/log/btmp {
   missingok
   monthly
   create 0600 root utmp
   rotate 1
}

但是在預計日誌會輪換的周末,情況並非如此。新的空文件被創建但仍然是空的,而最後一個旋轉的文件繼續增長。然後,我必須重新啟動httpd服務才能再次開始記錄。

問題是什麼?

您的日誌未輪換,因為它們的大小尚未達到 50 MB。

因為您指定了 a size,所以正常的基於日期的日誌輪換不會生效。相反,當日誌超過指定大小時,會輪換日誌,即使該時間需要超過一周或一個月或其他指定的任何時間段。

如手冊頁所示:

size size
       Log files are rotated only if they grow bigger than size bytes.

如果您想每週輪換日誌,但如果超過 50 MB 時又想更快maxsize 50M地輪換,那麼使用.

maxsize size
       Log files are rotated when they grow bigger than size bytes even before
       the additionally specified time interval (daily, weekly, monthly, or
       yearly). The related size option is similar except that it is mutually
       exclusive with the time interval options, and it causes log files to be
       rotated without regard for the last rotation time. When maxsize is used,
       both the size and timestamp of a log file are considered.

引用自:https://serverfault.com/questions/679263