Logrotate

logrotate : connf 中提到的大小 2000M 仍然日誌大小超過 2000M

  • December 28, 2015

我的 /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
}

# system-specific logs may be also be configured here.

我要輪換的日誌 /etc/logrotate.d/apc_rtbinfo.conf

/mnt/log/frengo/apc_rtbinfo.log {
daily
missingok
notifempty
size 2000M
compress
delaycompress
sharedscripts
copytruncate
rotate 3
}

logrotate 的輸出:

logrotate -v /etc/logrotate.d/apc_rtbinfo.conf
reading config file /etc/logrotate.d/apc_rtbinfo.conf
reading config info for /mnt/log/frengo/apc_rtbinfo.log

Handling 1 logs

rotating pattern: /mnt/log/frengo/apc_rtbinfo.log  2097152000 bytes (3 rotations)
empty log files are not rotated, old logs are removed
considering log /mnt/log/frengo/apc_rtbinfo.log
 log does not need rotating

我的旋轉日誌大小:

# du -sh /mnt/log/frengo/apc_rtbinfo.log*
0       /mnt/log/frengo/apc_rtbinfo.log
4.7G    /mnt/log/frengo/apc_rtbinfo.log.1
80M     /mnt/log/frengo/apc_rtbinfo.log.2
0       /mnt/log/frengo/apc_rtbinfo.log-20151222
679M    /mnt/log/frengo/apc_rtbinfo.log-20151225.gz
681M    /mnt/log/frengo/apc_rtbinfo.log-20151226.gz
691M    /mnt/log/frengo/apc_rtbinfo.log-20151227.gz
0       /mnt/log/frengo/apc_rtbinfo.log-20151228
70M     /mnt/log/frengo/apc_rtbinfo.log.2.gz
80M     /mnt/log/frengo/apc_rtbinfo.log.3
80M     /mnt/log/frengo/apc_rtbinfo.log.4

日誌旋轉輸出顯示“日誌不需要旋轉”,但我提到了“大小 2000M”,即如果日誌文件增長大於 2000M,則旋轉日誌文件,那麼“/mnt/log/frengo/apc_rtbinfo.log.1”是 4.7 GB

考慮的是目前日誌文件的大小 ( /mnt/log/frengo/apc_rtbinfo.log),該大小為 0。因此,無需旋轉。

至於較舊的日誌文件,logrotate不會持續監控文件,而是定期執行(每天,iirc)。如果在兩次執行之間,一個日誌文件變得異常大,直到下一次執行它才會發現。

如果空間非常寶貴,只需gzip在大文件上執行:

gzip /mnt/log/frengo/apc_rtbinfo.log

它將被一個壓縮的、小得多的文件替換。

您已經一起使用了sizedaily條件。但是,size和基於時間的條件是互斥的。您應該使用以下maxsize條件:

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://unix.stackexchange.com/questions/251948