patternbashModerate
logrotate: Preventing Log Files from Filling Disks
Viewed 0 times
logrotatelog rotationcompresspostrotateUSR1copytruncatedisk spacelog files
linux
Error Messages
Problem
Application log files grow indefinitely, eventually filling the disk and causing service failures.
Solution
Configure logrotate with appropriate rotation schedule, compression, and post-rotate scripts.
# Test logrotate config without running
logrotate --debug /etc/logrotate.d/myapp
# Force rotation (ignore schedule)
logrotate --force /etc/logrotate.d/myapp
# Example config: /etc/logrotate.d/myapp
# /var/log/myapp/*.log {
# daily
# rotate 14
# compress
# delaycompress
# missingok
# notifempty
# create 0640 www-data adm
# sharedscripts
# postrotate
# kill -USR1 $(cat /var/run/myapp.pid) 2>/dev/null || true
# endscript
# }
# Common options explained:
# daily/weekly/monthly = rotation frequency
# rotate N = keep N old files
# compress = gzip old files
# delaycompress = compress the previous rotation (not current)
# missingok = don't error if log file is missing
# notifempty = skip rotation if file is empty
# postrotate = run script after rotationWhy
Most apps keep file handles open to their log files. After logrotate renames the file, the app still writes to the old inode. The postrotate script sends a signal (SIGHUP or USR1) to make the app re-open its log file under the new name.
Gotchas
- Without a postrotate signal, the app continues writing to the renamed file — the new empty log file stays at 0 bytes.
delaycompressis needed because you can't compress the file the app is currently writing to.- logrotate state file is at /var/lib/logrotate/status — if corrupted, rotation may stop working.
- Symlinked log files require the
copytruncateoption instead of rename+signal, which has a small data loss window.
Revisions (0)
No revisions yet.