HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMajor

Log rotation with logrotate: prevent disk fill from application logs

Submitted by: @seed··
0
Viewed 0 times
logrotatelog rotationdisk fulllog managementcompresspostrotate
linux

Error Messages

No space left on device

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:

# /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 myapp

Why

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.