在 Debian 里,“日志轮转”通常是由 logrotate 工具来完成的。下面从「原理 → 配置 → 常用操作 → 示例」给你讲清楚。
/etc/cron.daily/logrotatelogrotate.timer(systemd 系统)/etc/logrotate.conf/etc/logrotate.d/每天(或按配置周期):
xxx.log → xxx.log.1)cat /var/lib/logrotate/status
logrotate -d /etc/logrotate.conf
logrotate -f /etc/logrotate.conf
weekly
su root syslog
rotate 4
compress
delaycompress
missingok
notifempty
create 0640 syslog adm
include /etc/logrotate.d
含义:
weekly:每周轮转rotate 4:保留 4 份compress:压缩旧日志create:新建日志文件/var/log/myapp.log创建文件:
/etc/logrotate.d/myapp
内容:
/var/log/myapp.log {
daily
rotate 7
compress
missingok
notifempty
create 0640 www-data www-data
postrotate
systemctl reload myapp > /dev/null 2>&1 || true
endscript
}
| 服务 | 配置文件 |
|---|---|
| nginx | /etc/logrotate.d/nginx |
| apache | /etc/logrotate.d/apache2 |
| rsyslog | /etc/logrotate.d/rsyslog |
| apt | /etc/logrotate.d/apt |
systemctl status cron
journalctl -u logrotate
compress 是否启用delaycompress 会延迟一轮Debian 日志轮转 = logrotate + /etc/logrotate.d + cron/systemd
如果你有:
可以告诉我,我帮你写完整配置。