Logrotate 不会删除旧日志

Sam*_*amK 14 linux logrotate

由于某种原因,旧的日志文件不会被删除。以 apache 为例

conf 文件说的是:

$ cat /etc/logrotate.d/apache2
/var/log/apache2/*.log {
    weekly
    missingok
    rotate 2
    compress
    delaycompress
    notifempty
    create 640 root adm
    sharedscripts
    postrotate
            /etc/init.d/apache2 reload > /dev/null
    endscript
}
Run Code Online (Sandbox Code Playgroud)

日志文件夹的(裁剪)内容:

# ls /var/log/apache2/
access.log       error.log.26.gz                other_vhosts_access.log.20.gz  access-ssl.log.14.gz
access.log.1     error.log.27.gz                other_vhosts_access.log.21.gz  access-ssl.log.15.gz
access.log.2.gz  error.log.28.gz                other_vhosts_access.log.22.gz  access-ssl.log.16.gz
access.log.3.gz  error.log.2.gz                 other_vhosts_access.log.23.gz  access-ssl.log.17.gz
[...]
Run Code Online (Sandbox Code Playgroud)

其实有很多:

# ls /var/log/apache2/ | wc -l
85
Run Code Online (Sandbox Code Playgroud)

带有 --verbose 的 logrotate 命令给了我这个:

# /usr/sbin/logrotate --verbose  /etc/logrotate.conf
[...]
reading config file apache2
reading config info for /var/log/apache2/*.log
[...]
rotating pattern: /var/log/apache2/*.log  weekly (2 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/apache2/access.log
  log does not need rotating
considering log /var/log/apache2/error.log
  log does not need rotating
considering log /var/log/apache2/other_vhosts_access.log
  log does not need rotating
considering log /var/log/apache2/pbpdhg-ssl.log
  log does not need rotating
not running postrotate script, since no logs were rotated
[...]
Run Code Online (Sandbox Code Playgroud)

这里有什么问题?日志已旋转但未删除?我错过了什么?

joh*_*n64 17

您的配置说:旋转 2

这意味着日志文件在被删除之前会旋转 2 次,因此 logrotate 只关心 2 个文件。

我的猜测是配置在某个时候发生了变化,因为以前保留了更多的日志文件,可能类似于旋转 28。这些旧文件您必须手动删除。

  • 因为rotate 2只处理2个文件,即.1.gz和.2.gz,忽略其他文件。 (2认同)