patternjavascriptMajor
Log rotation with logrotate: prevent disk fill from application logs
Viewed 0 times
logrotatelog rotationdisk fulllog managementcompresspostrotate
linux
Error Messages
Problem
Application log files grow indefinitely and eventually fill the disk, causing the application to crash or behave incorrectly.
Solution
Create a logrotate config for your application:
Test without executing:
# /etc/logrotate.d/myapp
/var/log/myapp/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
sharedscripts
postrotate
# Tell the app to reopen log files
systemctl reload myapp 2>/dev/null || true
# Or if using pm2:
# pm2 reloadLogs
endscript
}Test without executing:
logrotate -d /etc/logrotate.d/myapp
# Force rotation now
logrotate -f /etc/logrotate.d/myapp
# Check last run status
cat /var/lib/logrotate/status | grep myappWhy
delaycompress keeps the previous log uncompressed for one cycle so that log shippers (like filebeat) can finish reading before compression. postrotate ensures the app opens new file descriptors pointing to the new file.
Gotchas
- Without postrotate reload, the app continues writing to the rotated file (old inode), not the new empty one
- compress + delaycompress is the correct pairing — compress alone compresses the file the app might still be writing to
- logrotate runs daily via /etc/cron.daily — verify cron is running if rotation never happens
- For pm2, use pm2 install pm2-logrotate module instead for tighter integration
Revisions (0)
No revisions yet.