Without log rotation, log files grow indefinitely, eventually consuming all available disk space. logrotate automates compressing, archiving, and deleting old logs on a schedule.
Why Log Rotation Matters
An application or service logging continuously can generate gigabytes of data over time — without rotation, this eventually fills the disk, potentially causing the application (or the entire server) to fail when storage is exhausted.
logrotate Is Usually Pre-Installed
which logrotate
Most distributions include logrotate by default and already rotate common system logs (syslog, auth.log) automatically — this guide covers adding rotation for your own application's logs.
Step 1 — Create a Configuration File for Your Application
sudo nano /etc/logrotate.d/myapp
/var/log/myapp/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
create 0640 www-data www-data
}
Understanding Each Directive
| Directive | Meaning |
|---|---|
| daily | Rotate logs once per day |
| rotate 14 | Keep 14 rotated copies before deleting the oldest |
| compress | Gzip rotated logs to save space |
| delaycompress | Delay compression by one cycle, useful if a process might still be writing to the just-rotated file briefly |
| missingok | Don't error if the log file doesn't exist |
| notifempty | Skip rotation if the log file is empty |
| create | Create a new empty log file with specified permissions after rotation |
Step 2 — Test the Configuration
sudo logrotate -d /etc/logrotate.d/myapp
-d runs in debug/dry-run mode, showing what would happen without actually performing the rotation — verify the output matches your expectations before relying on it.
Step 3 — Force an Actual Rotation to Verify
sudo logrotate -f /etc/logrotate.d/myapp
Rotating Based on Size Instead of (or in Addition to) Time
/var/log/myapp/*.log {
size 100M
rotate 5
compress
}
Running a Command After Rotation (postrotate)
/var/log/myapp/*.log {
daily
rotate 14
compress
postrotate
systemctl reload myapp
endscript
}
Some applications hold a file handle to the log file and need a reload signal after rotation to start writing to the new file — check your specific application's logging behavior.
Verifying logrotate Runs on Schedule
cat /etc/cron.daily/logrotate
logrotate typically runs via a daily cron job already configured system-wide; your custom configuration file is automatically picked up without additional scheduling needed.
Checking Rotation History
cat /var/lib/logrotate/status
Shows when each configured log was last rotated.
Common Errors
Log file grows despite logrotate configuration — verify the application actually reopens/recreates its log file handle after rotation (via postrotate or the application's own log-reopening behavior); otherwise it may keep writing to the old, now-renamed file indefinitely.
Continue Reading
- How to Check and Manage Disk Usage on a Linux VPS
- How to Read and Analyze Linux Logs with journalctl
- Structured Logging Best Practices for Easier Debugging
Browse more articles in Linux Server Administration.