VPS Tutorials
Configure logrotate: retention, copytruncate and Docker logs
Understand logrotate's rotation counts, time and size rules, copytruncate's loss window, dated archives, permissions and reopen scripts. Verify scheduled and manual runs, then set separate journald and Docker log budgets and diagnose full-disk failures.

When df -h reports a full filesystem and /var/log contains large files, inspect the logs before deleting anything. Uncontrolled logging is one possible cause; deleted files held open by a process, container storage and the system journal need different treatment.
logrotate archives ordinary log files when a scheduler invokes it. It is not a daemon watching file sizes continuously. This guide explains the configuration, the tradeoffs of copying versus renaming, and the separate controls for Docker and journald.
# 系统自带的配置(不要改这些,它们是发行版维护的)
ls /etc/logrotate.d/
# 全局默认值
cat /etc/logrotate.conf
# 查看某个配置的最终生效结果(最有用的命令)
logrotate -d /etc/logrotate.d/nginx
-d is debug mode: it describes decisions without rotating files or updating the state file. A standalone rule may not inherit defaults from the global configuration, so also inspect the complete configuration when assessing production behavior.
The following commands actually process eligible logs; the force option rotates even when normal conditions are unmet. Schedule any disruptive reopen/reload operation deliberately:
# 强制执行一次(测试用,会真的转)
sudo logrotate -f /etc/logrotate.d/myapp
# 只看某个文件的详细信息
sudo logrotate -v /etc/logrotate.d/myapp
Inspect package-managed rules in /etc/logrotate.d/, including apt, dpkg, nginx and rsyslog. Prefer a separate rule for your own service rather than editing a vendor file or creating a duplicate rule for the same log.
/var/log/myapp/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
create 0640 www-data adm
sharedscripts
postrotate
systemctl reload myapp > /dev/null 2>&1 || true
endscript
}
| Directive | Meaning |
|---|---|
/var/log/myapp/*.log | Files to rotate; a wildcard can match several logs |
daily | Daily eligibility, evaluated when logrotate runs |
rotate 14 | Retain 14 archives |
compress | Compress archives |
delaycompress | Delay compression by one rotation |
missingok | Skip a missing file without an error |
notifempty | Skip empty logs |
create 0640 www-data adm | Create the replacement with these permissions and owners |
postrotate...endscript | Run the configured post-rotation script |
rotate N counts archives, not days. daily with rotate 14 often represents about 14 daily rotations; skipped/late runs and empty files mean it is not an exact 14-day policy. weekly with rotate 14 represents about 14 weeks, roughly three months.
compress with delaycompress is useful when a writer may temporarily retain the renamed file. Compression must not race a process still writing to that archive. In copytruncate mode the writer retains the original inode; delaying compression is not a substitute for understanding the copy/truncate race.
notifempty avoids empty archives; ifempty permits rotation of empty files. missingok suppresses a missing-file error, while nomissingok reports it. A logrotate error may be useful if a supposedly active service should always have a log, so choose deliberately.
Set create to match the application's real user and group. For example, www-data may require write access while 0640 restricts access to the owner and group. Check directory permissions too; a writable file inside an inaccessible directory is insufficient.
Normal rotation renames app.log to an archive such as app.log.1, creates a replacement, then uses postrotate to ask the application to reopen its logs. A process without reopen support can keep writing to the renamed file instead.
copytruncate copies the current file and truncates the original, keeping the writer's file descriptor attached to the same inode. Writes between copying and truncation can be lost. It is unsuitable when complete audit records are required; prefer an application's supported reopen mechanism.
| Service | Typical reopen mechanism; confirm the installed configuration |
|---|---|
| Nginx | nginx -s reopen or kill -USR1 $(cat /run/nginx.pid) |
| Apache | apachectl graceful |
| PHP-FPM | kill -USR1 $(cat /run/php/php8.3-fpm.pid); adapt the version-specific PID path |
| Custom service | Implement SIGHUP handling or a working systemctl reload action |
Use copytruncate only after confirming the application cannot reopen safely, and document the reason. The create directive has no effect with copytruncate because the original file remains in place.
| Configuration | Eligibility when the scheduler runs |
|---|---|
size 100M | Size-based; mutually exclusive with a time interval, with the later applicable directive taking precedence |
daily and maxsize 100M | Daily rotation, or earlier if the file exceeds 100M |
daily and minsize 100M | Both the time interval and minimum size must be satisfied |
- Stable logs with periodic archives:
dailyorweekly. - Variable, rapidly growing logs:
dailyplusmaxsize 500M. - Low-volume logs:
weeklyplusminsize 1M.
Writing size 10M does not create a continuous 10M cap. If the job runs daily, the file may grow far beyond that before the next check. More frequent scheduling can rotate size-triggered logs more often; use an appropriate state file, avoid overlapping invocations and choose archive names that support repeated rotations.
Reduce excessive logging at its source. journald RateLimit settings and Docker max-size address different logging paths. Start with the log troubleshooting guide to find the noisy service.
Numbered names such as app.log.1 and app.log.2.gz show rotation order. .3.gz does not necessarily mean three days ago if runs were skipped or size triggers added rotations.
dateext selects dated names:
/var/log/myapp/*.log {
daily
dateext
dateformat -%Y%m%d
rotate 30
}
Names such as app.log-20260929 and app.log-20260928 are easier to search by date. In dateformat, supported tokens include %Y, %m, %d and %s. Check the installed manual: this is a restricted format, not every strftime token. In dateformat, avoid : where portability matters, and preserve a lexically sortable date format.
dateyesterday uses the previous day's date. dateyesterday can suit a job archiving yesterday's output after midnight, but choose it according to the actual schedule. Daily-only suffixes can collide when you force or schedule multiple rotations in one day.
A successful rename does not prove new output reaches the replacement file. Configure and test the reopen action:
/var/log/nginx/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
create 0640 www-data adm
sharedscripts
postrotate
[ -f /run/nginx.pid ] && kill -USR1 $(cat /run/nginx.pid)
endscript
}
sharedscripts makes a group matched by *.log run postrotate once rather than once per file. With sharedscripts, verify the script handles the whole group correctly.
The [ -f /run/nginx.pid ] && guard avoids sending kill to a missing PID file, but this exact example can still return a nonzero status when the guard is false. Use an explicit conditional for an expected stopped service, and report unexpected reopen failures instead of hiding every error. endscript must end the script on its own line.
sudo logrotate -d /etc/logrotate.d/myapp
Read the actual eligibility decision, last-rotation timestamp and intended filename. These are contrasting examples of a skipped run and a rotation:
considering log /var/log/myapp/app.log
Now: 2026-09-29 03:00
Last rotated at 2026-09-28 03:00
log does not need rotating (log has already been rotated)
rotating log /var/log/myapp/app.log, log->rotateCount is 14
renaming /var/log/myapp/app.log.14.gz to /var/log/myapp/app.log.15.gz
...
A renaming message in debug output describes a planned rename; it does not mean debug mode changed the file. After a deliberate real run, verify archive contents, ownership and subsequent application writes.
# 一次检查所有配置(dry-run,不改动任何东西)
sudo logrotate -d /etc/logrotate.conf 2>&1 | less
Some systems record logrotate errors in /var/log/syslog:
grep logrotate /var/log/syslog | tail -20
Others use the journal, so also inspect journalctl -u logrotate.service. Common failures include syntax, permissions and a nonzero postrotate script.
The logs shown by journalctl use journald's own retention:
# 看当前占用
journalctl --disk-usage
# 手动清理到 500M
sudo journalctl --vacuum-size=500M
# 按时间清理
sudo journalctl --vacuum-time=7d
Vacuuming removes archived journal files, not necessarily the active file. Rotate the journal first when needed, and keep logs required for an incident or retention policy.
sudo sed -i 's/^#\?SystemMaxUse=.*/SystemMaxUse=500M/' /etc/systemd/journald.conf
sudo systemctl restart systemd-journald
That sed command changes an existing matching line only. If none exists, add the setting explicitly, preferably in a journald configuration drop-in, then restart the service deliberately.
| Setting | Purpose |
|---|---|
SystemMaxUse= | Persistent journal space budget |
SystemKeepFree= | Space to leave free on the filesystem |
MaxRetentionSec= | Maximum retention age, for example 1month |
SystemMaxFileSize= | Maximum individual journal file size |
SystemMaxUse is useful, but it is not the only valid way to limit retention. Defaults are percentage-based and distribution-dependent; volatile storage uses RuntimeMaxUse instead. Rate limiting controls message volume and does not replace a disk budget.
docker logs follows the configured Docker logging driver, not your application's ordinary file rule:
# 看哪个容器占得最多
sudo du -sh /var/lib/docker/containers/*/*-json.log | sort -h | tail -10
This path inspection applies to json-file logs, not every driver or rootless data directory. Do not edit Docker-managed active log files directly.
Merge logging defaults into /etc/docker/daemon.json, preserving existing daemon settings:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "20m",
"max-file": "5"
}
}
sudo systemctl restart docker
Restarting Docker can affect running workloads. max-size and max-file give the example an approximate 20M-per-file, 5-file budget, around 100M per container, with implementation/rotation overhead. Existing containers keep their creation-time logging configuration; docker compose up -d --force-recreate applies new settings by recreation, so confirm persistent data first.
Per-service Compose configuration is another option:
services:
app:
logging:
driver: json-file
options:
max-size: "20m"
max-file: "5"
See Docker disk cleanup for container storage and old-log diagnosis.
Some Java services and logging libraries already rotate their own files. Configure one owner for rotation rather than having the application and logrotate race over the same files.
/var/log/nginx/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
create 0640 www-data adm
sharedscripts
prerotate
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi
endscript
postrotate
[ -f /run/nginx.pid ] && kill -USR1 $(cat /run/nginx.pid)
endscript
}
Check ls /etc/logrotate.d/ | grep nginx before installing a second rule. Distribution packages often provide one already. Confirm the PID path, owners and graceful reopen behavior; the example guard needs the stopped-service handling discussed above.
/srv/app/shared/logs/*.log {
daily
rotate 30
dateext
dateformat -%Y%m%d
compress
delaycompress
missingok
notifempty
create 0640 appuser appuser
sharedscripts
postrotate
systemctl reload myapp > /dev/null 2>&1 || true
endscript
}
The || true at the end of postrotate suppresses failures from systemctl reload. || true may hide a genuine reload error too. Prefer an explicit check for an expected inactive service, then make unexpected failures observable.
/var/log/bigapp/*.log {
daily
maxsize 500M
rotate 7
compress
copytruncate
notifempty
}
daily plus maxsize 500M permits earlier size-based rotation at the next invocation, not immediately when the file reaches 500M. The copytruncate loss window still applies.
# 强制转一次(不管条件是否满足)
sudo logrotate -f /etc/logrotate.d/myapp
# 只看会不会转,不实际执行
sudo logrotate -d /etc/logrotate.d/myapp
# 详细模式,实际执行
sudo logrotate -v /etc/logrotate.d/myapp
-f is useful for a planned real test or an urgent rotation after checking available space. Repeated -f consumes archive slots rapidly and bypasses normal daily eligibility. Copying/compression can need additional disk space, so a full filesystem requires care first.
No replacement log: inspect create and whether the application creates its own file. With copytruncate, the original is retained instead.
The writer still uses the archive: postrotate did not make it reopen, or the signal/PID is wrong. Check subsequent writes and the service's supported behavior.
Too many archives: inspect the effective rotate, included defaults and duplicate rules. A default of zero means no retained rotations, but the global file may override it. rotate 52 represents 52 rotations, not automatically a year. compress saves space but depends on log compressibility.
logrotate: error: stat of /path failed: No such file or directory: confirm the path; add missingok only if an absent file is expected.
error: skipping "/path" because parent directory has insecure permissions: inspect the parent directory and owner. 755 may suit some directories, but application-user-owned writable paths may need a correct su directive rather than indiscriminately changing ownership.
Nothing runs: inspect the real scheduler:
systemctl list-timers | grep logrotate
cat /etc/cron.daily/logrotate
Some Debian/Ubuntu installations use a timer; others use /etc/cron.daily/logrotate, which may skip work when systemd handles scheduling. Check both without scheduling duplicates. See cron and timers. logrotate reads its configuration on invocation; it needs no daemon restart.
/var/log is already full: first identify safe, disposable archives and preserve incident evidence. The following emergency example contains a deletion command: inspect matching files before running its delete step, and do not treat every archive older than 30 days as disposable.
1. 先清出空间(不然什么操作都可能失败)
sudo journalctl --vacuum-size=200M
sudo find /var/log -name "*.gz" -mtime +30 -delete
2. 再配好 logrotate 防止复发
Follow the full-disk recovery order. Vacuum may not release active journal space, and forced copy-based rotation may initially require more free space.
Deleting a log freed no space: find the process retaining the deleted file:
# 找出持有已删除文件的进程
sudo lsof +L1 | head -20
# 正确的做法是重启那个服务,让它重新打开日志
sudo systemctl restart myapp
Avoid rm on actively written logs. truncate -s 0 also destroys current content and is not a routine retention strategy; use supported reopen/rotation, or a deliberate service restart after saving necessary evidence.
Separate log partition: isolating /var/log can protect the root filesystem, but it still needs retention and alerts. For a small VPS, a working rotation policy and SystemMaxUse may be enough.
- Keep custom rules in
/etc/logrotate.d/, without duplicate paths. - Interpret
rotate Nas a rotation count. - Make
postrotatereopen the application's logs successfully. - Use
sharedscriptsfor a group-level script where appropriate. - Choose
missingokandnotifemptydeliberately. - Confirm
create, directory access and file ownership. - Run
logrotate -dand inspect the decisions. - Set an appropriate journald budget, including
SystemMaxUsefor persistent storage. - Configure Docker
max-sizeandmax-filefor the chosen driver. - Enable disk and traffic alerts.
Retention limits help prevent growth; an alert tells you when a rule stopped working or an unexpected writer appeared. Verify both.