What Is Logrotate?
Logrotate is a utility that automatically rotates, compresses, and removes old log files. Without it, log files grow indefinitely and consume all disk space.
Default Configuration
# Global settings
cat /etc/logrotate.conf
# Per-application configs
ls /etc/logrotate.d/Create a Custom Configuration
Create /etc/logrotate.d/myapp:
/var/log/myapp/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
create 0640 www-data www-data
sharedscripts
postrotate
systemctl reload myapp > /dev/null 2>&1 || true
endscript
}Key Directives
| Directive | Meaning |
|---|---|
daily/weekly/monthly | Rotation frequency |
rotate 14 | Keep 14 rotated files |
compress | Gzip old log files |
delaycompress | Compress previous rotation, not current |
missingok | Don't error if log is missing |
notifempty | Don't rotate empty files |
create | Create new log file with specified permissions |
copytruncate | Truncate original file after copy (apps that don't reopen files) |
size 100M | Rotate when file reaches 100 MB |
Test Configuration
# Dry run (show what would happen)
sudo logrotate -d /etc/logrotate.d/myapp
# Force rotation
sudo logrotate -f /etc/logrotate.d/myappSize-Based Rotation
/var/log/myapp/access.log {
size 100M
rotate 5
compress
missingok
copytruncate
}