![logrotate 不分割日志文件](https://linux22.com/image/181280/logrotate%20%E4%B8%8D%E5%88%86%E5%89%B2%E6%97%A5%E5%BF%97%E6%96%87%E4%BB%B6.png)
我正在尝试让 logrotate 在我的 OpenResty 服务器日志上工作。我的 logrotate 配置文件/etc/logrotate.d/openresty
应该每天执行,这是默认行为,我正在尝试使用sudo logrotate -vfd /etc/logrotate.d/openresty
.
这就是我的配置方式:
/home/appname/logs/openresty/*.log /var/log/openresty/*.log {
su root root
size 100M
missingok
rotate 14
compress
delaycompress
notifempty
create 0660 root root
sharedscripts
prerotate
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi \
endscript
}
但这就是我的日志目录 ( ~/logs/openresty
) 的样子:
total 1.3G
-rw-r----- 1 root root 1.2G Jul 23 09:41 access.log.1
-rw-r----- 1 www-data adm 154K Jul 13 11:43 access.log.2.gz
-rw-r----- 1 www-data adm 159M Jul 13 11:18 access.log.3.gz
-rw-r----- 1 root root 572K Oct 5 04:24 error.log
-rw-r----- 1 root root 62K Jul 23 02:52 error.log.1
-rw-r----- 1 www-data adm 12K Jul 13 10:55 error.log.2.gz
正如您所看到的,主日志文件远高于 100 MB 的限制,并且它没有按应有的方式进行分割。也不知道为什么它被命名access.log.1
而不是access.log
(这是它在服务器中的配置方式)。
当我运行时,sudo logrotate -v /etc/logrotate.d/openresty
我得到以下输出:
reading config file /etc/logrotate.d/openresty
Handling 1 logs
rotating pattern: /home/appname/logs/openresty/*.log /var/log/openresty/*.log 104857600 bytes (14 rotations)
empty log files are not rotated, old logs are removed
considering log /home/appname/logs/openresty/error.log
log does not need rotating
considering log /var/log/openresty/access.log
log needs rotating
considering log /var/log/openresty/error.log
log does not need rotating
rotating log /var/log/openresty/access.log, log->rotateCount is 14
dateext suffix '-20201005'
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/openresty/access.log.14.gz to /var/log/openresty/access.log.15.gz (rotatecount 14, logstart 1, i 14),
old log /var/log/openresty/access.log.14.gz does not exist
renaming /var/log/openresty/access.log.13.gz to /var/log/openresty/access.log.14.gz (rotatecount 14, logstart 1, i 13),
old log /var/log/openresty/access.log.13.gz does not exist
renaming /var/log/openresty/access.log.12.gz to /var/log/openresty/access.log.13.gz (rotatecount 14, logstart 1, i 12),
old log /var/log/openresty/access.log.12.gz does not exist
renaming /var/log/openresty/access.log.11.gz to /var/log/openresty/access.log.12.gz (rotatecount 14, logstart 1, i 11),
old log /var/log/openresty/access.log.11.gz does not exist
renaming /var/log/openresty/access.log.10.gz to /var/log/openresty/access.log.11.gz (rotatecount 14, logstart 1, i 10),
old log /var/log/openresty/access.log.10.gz does not exist
renaming /var/log/openresty/access.log.9.gz to /var/log/openresty/access.log.10.gz (rotatecount 14, logstart 1, i 9),
old log /var/log/openresty/access.log.9.gz does not exist
renaming /var/log/openresty/access.log.8.gz to /var/log/openresty/access.log.9.gz (rotatecount 14, logstart 1, i 8),
old log /var/log/openresty/access.log.8.gz does not exist
renaming /var/log/openresty/access.log.7.gz to /var/log/openresty/access.log.8.gz (rotatecount 14, logstart 1, i 7),
old log /var/log/openresty/access.log.7.gz does not exist
renaming /var/log/openresty/access.log.6.gz to /var/log/openresty/access.log.7.gz (rotatecount 14, logstart 1, i 6),
old log /var/log/openresty/access.log.6.gz does not exist
renaming /var/log/openresty/access.log.5.gz to /var/log/openresty/access.log.6.gz (rotatecount 14, logstart 1, i 5),
old log /var/log/openresty/access.log.5.gz does not exist
renaming /var/log/openresty/access.log.4.gz to /var/log/openresty/access.log.5.gz (rotatecount 14, logstart 1, i 4),
old log /var/log/openresty/access.log.4.gz does not exist
renaming /var/log/openresty/access.log.3.gz to /var/log/openresty/access.log.4.gz (rotatecount 14, logstart 1, i 3),
renaming /var/log/openresty/access.log.2.gz to /var/log/openresty/access.log.3.gz (rotatecount 14, logstart 1, i 2),
renaming /var/log/openresty/access.log.1.gz to /var/log/openresty/access.log.2.gz (rotatecount 14, logstart 1, i 1),
renaming /var/log/openresty/access.log.0.gz to /var/log/openresty/access.log.1.gz (rotatecount 14, logstart 1, i 0),
old log /var/log/openresty/access.log.0.gz does not exist
log /var/log/openresty/access.log.15.gz doesn't exist -- won't try to dispose of it
running prerotate script
renaming /var/log/openresty/access.log to /var/log/openresty/access.log.1
creating new /var/log/openresty/access.log mode = 0660 uid = 0 gid = 0
然而,绝对没有任何东西会旋转~/logs/openresty
。它只是忽略了整个事情。
并且创建了/var/log/openresty
一个新的空日志access.log
,但旧的日志没有被分割,并且仍然超过 100 MB:
total 379M
-rw-rw---- 1 root root 0 Oct 5 08:51 access.log
-rw-r----- 1 root root 366M Oct 5 08:53 access.log.1
-rw-r----- 1 root root 5.1M Jul 23 09:41 access.log.2.gz
-rw-r----- 1 www-data adm 8.0K Jul 13 11:42 access.log.3.gz
-rw-r----- 1 root root 8.2M Jul 13 11:18 access.log.4.gz
-rw-r----- 1 www-data adm 70K Oct 4 14:17 error.log
-rw-r----- 1 root root 948 Jun 26 10:24 error.log.1
此外,由于某种原因,服务器继续写入access.log.1
而不是access.log
.
答案1
Logrotate 正在工作,但 openresty 不知道它应该写入新的日志文件(因此它不断写入access.log.1
)。
您需要将postrotate
脚本添加到您的配置中:
postrotate
/usr/local/bin/openresty -s reload
endscript
(我不熟悉 openresty,所以这openresty -s reload
只是一个猜测 - 你需要在你的文档中查找它)