我的 Ubuntu 14.04 机器上的 apache 日志文件似乎/var/log/apache2/
没有轮换。我以前遇到过这个问题,不得不手动轮换日志文件,但看起来它仍然没有自动轮换。
我尝试sudo logrotate -d /etc/logrotate.conf
手动轮换日志,以防 cron 出现问题。我还以 root 身份运行此程序,以避免出现权限问题。尽管输出看起来确实如此,但仍然没有轮换。以下是logrotate
返回的相关信息:
rotating pattern: /var/log/apache2/*.log weekly (52 rotations)
empty log files are not rotated, old logs are removed
switching euid to 0 and egid to 104
considering log /var/log/apache2/access.log
log needs rotating
considering log /var/log/apache2/error.log
log needs rotating
rotating log /var/log/apache2/access.log, log->rotateCount is 52
dateext suffix '-20160404'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
compressing log with: /bin/gzip
renaming /var/log/apache2/access.log.52.gz to /var/log/apache2/access.log.53.gz (rotatecount 52, logstart 1, i 52),
renaming /var/log/apache2/access.log.51.gz to /var/log/apache2/access.log.52.gz (rotatecount 52, logstart 1, i 51),
renaming /var/log/apache2/access.log.50.gz to /var/log/apache2/access.log.51.gz (rotatecount 52, logstart 1, i 50),
renaming /var/log/apache2/access.log.49.gz to /var/log/apache2/access.log.50.gz (rotatecount 52, logstart 1, i 49),
renaming /var/log/apache2/access.log.48.gz to /var/log/apache2/access.log.49.gz (rotatecount 52, logstart 1, i 48),
renaming /var/log/apache2/access.log.47.gz to /var/log/apache2/access.log.48.gz (rotatecount 52, logstart 1, i 47),
renaming /var/log/apache2/access.log.46.gz to /var/log/apache2/access.log.47.gz (rotatecount 52, logstart 1, i 46),
renaming /var/log/apache2/access.log.45.gz to /var/log/apache2/access.log.46.gz (rotatecount 52, logstart 1, i 45),
renaming /var/log/apache2/access.log.44.gz to /var/log/apache2/access.log.45.gz (rotatecount 52, logstart 1, i 44),
renaming /var/log/apache2/access.log.43.gz to /var/log/apache2/access.log.44.gz (rotatecount 52, logstart 1, i 43),
renaming /var/log/apache2/access.log.42.gz to /var/log/apache2/access.log.43.gz (rotatecount 52, logstart 1, i 42),
renaming /var/log/apache2/access.log.41.gz to /var/log/apache2/access.log.42.gz (rotatecount 52, logstart 1, i 41),
renaming /var/log/apache2/access.log.40.gz to /var/log/apache2/access.log.41.gz (rotatecount 52, logstart 1, i 40),
renaming /var/log/apache2/access.log.39.gz to /var/log/apache2/access.log.40.gz (rotatecount 52, logstart 1, i 39),
renaming /var/log/apache2/access.log.38.gz to /var/log/apache2/access.log.39.gz (rotatecount 52, logstart 1, i 38),
renaming /var/log/apache2/access.log.37.gz to /var/log/apache2/access.log.38.gz (rotatecount 52, logstart 1, i 37),
renaming /var/log/apache2/access.log.36.gz to /var/log/apache2/access.log.37.gz (rotatecount 52, logstart 1, i 36),
renaming /var/log/apache2/access.log.35.gz to /var/log/apache2/access.log.36.gz (rotatecount 52, logstart 1, i 35),
renaming /var/log/apache2/access.log.34.gz to /var/log/apache2/access.log.35.gz (rotatecount 52, logstart 1, i 34),
renaming /var/log/apache2/access.log.33.gz to /var/log/apache2/access.log.34.gz (rotatecount 52, logstart 1, i 33),
renaming /var/log/apache2/access.log.32.gz to /var/log/apache2/access.log.33.gz (rotatecount 52, logstart 1, i 32),
renaming /var/log/apache2/access.log.31.gz to /var/log/apache2/access.log.32.gz (rotatecount 52, logstart 1, i 31),
renaming /var/log/apache2/access.log.30.gz to /var/log/apache2/access.log.31.gz (rotatecount 52, logstart 1, i 30),
renaming /var/log/apache2/access.log.29.gz to /var/log/apache2/access.log.30.gz (rotatecount 52, logstart 1, i 29),
renaming /var/log/apache2/access.log.28.gz to /var/log/apache2/access.log.29.gz (rotatecount 52, logstart 1, i 28),
renaming /var/log/apache2/access.log.27.gz to /var/log/apache2/access.log.28.gz (rotatecount 52, logstart 1, i 27),
renaming /var/log/apache2/access.log.26.gz to /var/log/apache2/access.log.27.gz (rotatecount 52, logstart 1, i 26),
renaming /var/log/apache2/access.log.25.gz to /var/log/apache2/access.log.26.gz (rotatecount 52, logstart 1, i 25),
renaming /var/log/apache2/access.log.24.gz to /var/log/apache2/access.log.25.gz (rotatecount 52, logstart 1, i 24),
renaming /var/log/apache2/access.log.23.gz to /var/log/apache2/access.log.24.gz (rotatecount 52, logstart 1, i 23),
renaming /var/log/apache2/access.log.22.gz to /var/log/apache2/access.log.23.gz (rotatecount 52, logstart 1, i 22),
renaming /var/log/apache2/access.log.21.gz to /var/log/apache2/access.log.22.gz (rotatecount 52, logstart 1, i 21),
renaming /var/log/apache2/access.log.20.gz to /var/log/apache2/access.log.21.gz (rotatecount 52, logstart 1, i 20),
renaming /var/log/apache2/access.log.19.gz to /var/log/apache2/access.log.20.gz (rotatecount 52, logstart 1, i 19),
renaming /var/log/apache2/access.log.18.gz to /var/log/apache2/access.log.19.gz (rotatecount 52, logstart 1, i 18),
renaming /var/log/apache2/access.log.17.gz to /var/log/apache2/access.log.18.gz (rotatecount 52, logstart 1, i 17),
renaming /var/log/apache2/access.log.16.gz to /var/log/apache2/access.log.17.gz (rotatecount 52, logstart 1, i 16),
renaming /var/log/apache2/access.log.15.gz to /var/log/apache2/access.log.16.gz (rotatecount 52, logstart 1, i 15),
renaming /var/log/apache2/access.log.14.gz to /var/log/apache2/access.log.15.gz (rotatecount 52, logstart 1, i 14),
renaming /var/log/apache2/access.log.13.gz to /var/log/apache2/access.log.14.gz (rotatecount 52, logstart 1, i 13),
renaming /var/log/apache2/access.log.12.gz to /var/log/apache2/access.log.13.gz (rotatecount 52, logstart 1, i 12),
renaming /var/log/apache2/access.log.11.gz to /var/log/apache2/access.log.12.gz (rotatecount 52, logstart 1, i 11),
renaming /var/log/apache2/access.log.10.gz to /var/log/apache2/access.log.11.gz (rotatecount 52, logstart 1, i 10),
renaming /var/log/apache2/access.log.9.gz to /var/log/apache2/access.log.10.gz (rotatecount 52, logstart 1, i 9),
renaming /var/log/apache2/access.log.8.gz to /var/log/apache2/access.log.9.gz (rotatecount 52, logstart 1, i 8),
renaming /var/log/apache2/access.log.7.gz to /var/log/apache2/access.log.8.gz (rotatecount 52, logstart 1, i 7),
renaming /var/log/apache2/access.log.6.gz to /var/log/apache2/access.log.7.gz (rotatecount 52, logstart 1, i 6),
renaming /var/log/apache2/access.log.5.gz to /var/log/apache2/access.log.6.gz (rotatecount 52, logstart 1, i 5),
renaming /var/log/apache2/access.log.4.gz to /var/log/apache2/access.log.5.gz (rotatecount 52, logstart 1, i 4),
renaming /var/log/apache2/access.log.3.gz to /var/log/apache2/access.log.4.gz (rotatecount 52, logstart 1, i 3),
renaming /var/log/apache2/access.log.2.gz to /var/log/apache2/access.log.3.gz (rotatecount 52, logstart 1, i 2),
renaming /var/log/apache2/access.log.1.gz to /var/log/apache2/access.log.2.gz (rotatecount 52, logstart 1, i 1),
renaming /var/log/apache2/access.log.0.gz to /var/log/apache2/access.log.1.gz (rotatecount 52, logstart 1, i 0),
rotating log /var/log/apache2/error.log, log->rotateCount is 52
dateext suffix '-20160404'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
compressing log with: /bin/gzip
renaming /var/log/apache2/error.log.52.gz to /var/log/apache2/error.log.53.gz (rotatecount 52, logstart 1, i 52),
renaming /var/log/apache2/error.log.51.gz to /var/log/apache2/error.log.52.gz (rotatecount 52, logstart 1, i 51),
renaming /var/log/apache2/error.log.50.gz to /var/log/apache2/error.log.51.gz (rotatecount 52, logstart 1, i 50),
renaming /var/log/apache2/error.log.49.gz to /var/log/apache2/error.log.50.gz (rotatecount 52, logstart 1, i 49),
renaming /var/log/apache2/error.log.48.gz to /var/log/apache2/error.log.49.gz (rotatecount 52, logstart 1, i 48),
renaming /var/log/apache2/error.log.47.gz to /var/log/apache2/error.log.48.gz (rotatecount 52, logstart 1, i 47),
renaming /var/log/apache2/error.log.46.gz to /var/log/apache2/error.log.47.gz (rotatecount 52, logstart 1, i 46),
renaming /var/log/apache2/error.log.45.gz to /var/log/apache2/error.log.46.gz (rotatecount 52, logstart 1, i 45),
renaming /var/log/apache2/error.log.44.gz to /var/log/apache2/error.log.45.gz (rotatecount 52, logstart 1, i 44),
renaming /var/log/apache2/error.log.43.gz to /var/log/apache2/error.log.44.gz (rotatecount 52, logstart 1, i 43),
renaming /var/log/apache2/error.log.42.gz to /var/log/apache2/error.log.43.gz (rotatecount 52, logstart 1, i 42),
renaming /var/log/apache2/error.log.41.gz to /var/log/apache2/error.log.42.gz (rotatecount 52, logstart 1, i 41),
renaming /var/log/apache2/error.log.40.gz to /var/log/apache2/error.log.41.gz (rotatecount 52, logstart 1, i 40),
renaming /var/log/apache2/error.log.39.gz to /var/log/apache2/error.log.40.gz (rotatecount 52, logstart 1, i 39),
renaming /var/log/apache2/error.log.38.gz to /var/log/apache2/error.log.39.gz (rotatecount 52, logstart 1, i 38),
renaming /var/log/apache2/error.log.37.gz to /var/log/apache2/error.log.38.gz (rotatecount 52, logstart 1, i 37),
renaming /var/log/apache2/error.log.36.gz to /var/log/apache2/error.log.37.gz (rotatecount 52, logstart 1, i 36),
renaming /var/log/apache2/error.log.35.gz to /var/log/apache2/error.log.36.gz (rotatecount 52, logstart 1, i 35),
renaming /var/log/apache2/error.log.34.gz to /var/log/apache2/error.log.35.gz (rotatecount 52, logstart 1, i 34),
renaming /var/log/apache2/error.log.33.gz to /var/log/apache2/error.log.34.gz (rotatecount 52, logstart 1, i 33),
renaming /var/log/apache2/error.log.32.gz to /var/log/apache2/error.log.33.gz (rotatecount 52, logstart 1, i 32),
renaming /var/log/apache2/error.log.31.gz to /var/log/apache2/error.log.32.gz (rotatecount 52, logstart 1, i 31),
renaming /var/log/apache2/error.log.30.gz to /var/log/apache2/error.log.31.gz (rotatecount 52, logstart 1, i 30),
renaming /var/log/apache2/error.log.29.gz to /var/log/apache2/error.log.30.gz (rotatecount 52, logstart 1, i 29),
renaming /var/log/apache2/error.log.28.gz to /var/log/apache2/error.log.29.gz (rotatecount 52, logstart 1, i 28),
renaming /var/log/apache2/error.log.27.gz to /var/log/apache2/error.log.28.gz (rotatecount 52, logstart 1, i 27),
renaming /var/log/apache2/error.log.26.gz to /var/log/apache2/error.log.27.gz (rotatecount 52, logstart 1, i 26),
renaming /var/log/apache2/error.log.25.gz to /var/log/apache2/error.log.26.gz (rotatecount 52, logstart 1, i 25),
renaming /var/log/apache2/error.log.24.gz to /var/log/apache2/error.log.25.gz (rotatecount 52, logstart 1, i 24),
renaming /var/log/apache2/error.log.23.gz to /var/log/apache2/error.log.24.gz (rotatecount 52, logstart 1, i 23),
renaming /var/log/apache2/error.log.22.gz to /var/log/apache2/error.log.23.gz (rotatecount 52, logstart 1, i 22),
renaming /var/log/apache2/error.log.21.gz to /var/log/apache2/error.log.22.gz (rotatecount 52, logstart 1, i 21),
renaming /var/log/apache2/error.log.20.gz to /var/log/apache2/error.log.21.gz (rotatecount 52, logstart 1, i 20),
renaming /var/log/apache2/error.log.19.gz to /var/log/apache2/error.log.20.gz (rotatecount 52, logstart 1, i 19),
renaming /var/log/apache2/error.log.18.gz to /var/log/apache2/error.log.19.gz (rotatecount 52, logstart 1, i 18),
renaming /var/log/apache2/error.log.17.gz to /var/log/apache2/error.log.18.gz (rotatecount 52, logstart 1, i 17),
renaming /var/log/apache2/error.log.16.gz to /var/log/apache2/error.log.17.gz (rotatecount 52, logstart 1, i 16),
renaming /var/log/apache2/error.log.15.gz to /var/log/apache2/error.log.16.gz (rotatecount 52, logstart 1, i 15),
renaming /var/log/apache2/error.log.14.gz to /var/log/apache2/error.log.15.gz (rotatecount 52, logstart 1, i 14),
renaming /var/log/apache2/error.log.13.gz to /var/log/apache2/error.log.14.gz (rotatecount 52, logstart 1, i 13),
renaming /var/log/apache2/error.log.12.gz to /var/log/apache2/error.log.13.gz (rotatecount 52, logstart 1, i 12),
renaming /var/log/apache2/error.log.11.gz to /var/log/apache2/error.log.12.gz (rotatecount 52, logstart 1, i 11),
renaming /var/log/apache2/error.log.10.gz to /var/log/apache2/error.log.11.gz (rotatecount 52, logstart 1, i 10),
renaming /var/log/apache2/error.log.9.gz to /var/log/apache2/error.log.10.gz (rotatecount 52, logstart 1, i 9),
renaming /var/log/apache2/error.log.8.gz to /var/log/apache2/error.log.9.gz (rotatecount 52, logstart 1, i 8),
renaming /var/log/apache2/error.log.7.gz to /var/log/apache2/error.log.8.gz (rotatecount 52, logstart 1, i 7),
renaming /var/log/apache2/error.log.6.gz to /var/log/apache2/error.log.7.gz (rotatecount 52, logstart 1, i 6),
renaming /var/log/apache2/error.log.5.gz to /var/log/apache2/error.log.6.gz (rotatecount 52, logstart 1, i 5),
renaming /var/log/apache2/error.log.4.gz to /var/log/apache2/error.log.5.gz (rotatecount 52, logstart 1, i 4),
renaming /var/log/apache2/error.log.3.gz to /var/log/apache2/error.log.4.gz (rotatecount 52, logstart 1, i 3),
renaming /var/log/apache2/error.log.2.gz to /var/log/apache2/error.log.3.gz (rotatecount 52, logstart 1, i 2),
renaming /var/log/apache2/error.log.1.gz to /var/log/apache2/error.log.2.gz (rotatecount 52, logstart 1, i 1),
renaming /var/log/apache2/error.log.0.gz to /var/log/apache2/error.log.1.gz (rotatecount 52, logstart 1, i 0),
rotating log /var/log/apache2/other_vhosts_access.log, log->rotateCount is 52
running prerotate script
running script with arg /var/log/apache2/*.log : "
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi; \
"
renaming /var/log/apache2/access.log to /var/log/apache2/access.log.1
creating new /var/log/apache2/access.log mode = 0640 uid = 0 gid = 4
renaming /var/log/apache2/error.log to /var/log/apache2/error.log.1
creating new /var/log/apache2/error.log mode = 0640 uid = 0 gid = 4
renaming /var/log/apache2/other_vhosts_access.log to /var/log/apache2/other_vhosts_access.log.1
creating new /var/log/apache2/other_vhosts_access.log mode = 0640 uid = 0 gid = 4
running postrotate script
running script with arg /var/log/apache2/*.log : "
if /etc/init.d/apache2 status > /dev/null ; then \
/etc/init.d/apache2 reload > /dev/null; \
fi;
"
removing old log /var/log/apache2/access.log.53.gz
error: error opening /var/log/apache2/access.log.53.gz: No such file or directory
switching euid to 0 and egid to 0
error.log 和 access.log 都存在同样的问题。如果你查看访问日志文件,你会注意到唯一被触及的是 access.log 文件,并且没有任何内容被轮换:
-rw-r----- 1 root adm 1.1G Apr 5 00:10 access.log
-rw-r----- 1 root adm 165M Dec 6 00:12 access.log.1
-rw-r----- 1 root adm 18M Oct 5 2015 access.log.10.gz
-rw-r----- 1 root adm 14M Sep 27 2015 access.log.11.gz
-rw-r----- 1 root adm 19M Sep 21 2015 access.log.12.gz
-rw-r----- 1 root adm 16M Sep 13 2015 access.log.13.gz
-rw-r----- 1 root adm 14M Sep 6 2015 access.log.14.gz
-rw-r----- 1 root adm 19M Aug 31 2015 access.log.15.gz
-rw-r----- 1 root adm 15M Aug 23 2015 access.log.16.gz
-rw-r----- 1 root adm 20M Aug 17 2015 access.log.17.gz
-rw-r----- 1 root adm 15M Aug 9 2015 access.log.18.gz
-rw-r----- 1 root adm 20M Aug 3 2015 access.log.19.gz
-rw-r----- 1 root adm 3.9M Jul 26 2015 access.log.20.gz
-rw-r----- 1 root adm 20M Nov 30 00:12 access.log.2.gz
-rw-r----- 1 root adm 15M Nov 22 00:12 access.log.3.gz
-rw-r----- 1 root adm 16M Nov 16 00:12 access.log.4.gz
-rw-r----- 1 root adm 15M Nov 8 00:12 access.log.5.gz
-rw-r----- 1 root adm 13M Nov 1 00:12 access.log.6.gz
-rw-r----- 1 root adm 18M Oct 26 00:12 access.log.7.gz
-rw-r----- 1 root adm 16M Oct 18 00:12 access.log.8.gz
-rw-r----- 1 root adm 14M Oct 11 00:12 access.log.9.gz
它甚至说在输出中写入:
renaming /var/log/apache2/access.log to /var/log/apache2/access.log.1
creating new /var/log/apache2/access.log mode = 0640 uid = 0 gid = 4
所以我很困惑,如果这显然发生了,为什么实际上什么都没有发生?有人能找到这个问题的解决方案吗?
编辑:如果相关的话,我的/etc/logrotate.d/apache2
文件如下所示:
/var/log/apache2/*.log {
weekly
missingok
rotate 52
compress
delaycompress
notifempty
create 640 root adm
sharedscripts
postrotate
if /etc/init.d/apache2 status > /dev/null ; then \
/etc/init.d/apache2 reload > /dev/null; \
fi;
endscript
prerotate
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi; \
endscript
}
当我手动运行 logrotate 时,sudo logrotate /etc/logrotate.conf
日志正确旋转。看起来就像我上面提到的-d
,导致我手动尝试时日志不旋转。这似乎现在是一个与 cron 有关的问题,而 logrotate 可能实际上没有在 cron 中运行?
查看我的 cron 文件夹,我确实有一个如下所示的logrotate
脚本:/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 每天都在运行,所以它不运行应该没有问题?
答案1
如果您根据 ls 命令显示的时间戳断言日志没有轮换,那么您的担心就是没有道理的。
重命名文件时,磁盘上的实际 inode 不会受到影响,因此时间戳不会改变。您基本上是在更改包含日志文件的目录的目录表,而不是更改书的页面。只有新创建的文件才会获得与其关联的新时间戳。
您可以使用以下方式确认
zcat access.log.2.gz | head
zcat access.log.2.gz | tail
确认每个文件中第一个和最后一个日志条目的时间戳。或者,您可以告诉 logrotate 在每个日志文件后附加一个时间戳,而不是数字。
将以下行添加到 /etc/logrotate.d/apache2
dateext
dateformat -%Y-%m-%d-%s
[更新]
虽然我之前的断言是正确的,但它取决于所使用的操作系统、文件系统和挂载选项。我刚刚检查了一些较新的服务器,时间戳与默认的 Ubuntu 14.04LTS 安装一致。
您是否有足够的可用空间:
df -h /var/log
文件系统是否偶然以只读方式挂载?您可以尝试向其中写入文件,也可以检查此命令的输出。并在输出中查找 rw 或 ro。
mount | grep
df /var/log/apache2/ | tail -1 | cut -d' ' -f1
在我的测试系统上,上述命令产生此输出。 () 中的第一个 rw 表示它是读/写的,而不是只读的。
/dev/xvda1 on / type ext4 (rw,noatime,errors=remount-ro,barrier=0)
gz 文件的大小并不令人意外。日志文件的压缩效果非常好,因为它们包含大量重复数据。
答案2
我觉得您似乎处于一个奇怪的环境中,或者 logrotate 配置不正确——您可以发布您的配置吗?也就是说,您已经声明您使用了选项-d
,这意味着“不做任何事情”——没有这个选项它还能工作吗?在这种情况下,您的 cron 或 cron 作业可能存在问题——cron 行/作业说了什么?
否则,我要检查的是:
- 验证 logrotate 是否配置正确——我不记得它是否支持这个功能,但它可能有一个选项(在配置中——见上文
-d
)用于启用“试运行”(即说明它会做,但没有实际上你会做吗? - 您和 logrotate 正在查看相同的路径,例如是否有可能有人错误配置了它并且它处于 chroot 中?
- 挂载点的行为是否正确,例如,如果这些文件被保存到完整的磁盘或网络共享,也许一些更改被拒绝;您可以手动执行某些更改,例如重命名文件吗?