logrotate
Overview
The logrotate
command manages log files by rotating, compressing, and mailing them. It helps prevent log files from consuming too much disk space.
Syntax
logrotate [options] config_file
Common Options
Option | Description |
---|---|
-d |
Debug mode |
-f |
Force rotation |
-m command |
Mail command |
-s statefile |
Use alternate state file |
-v |
Verbose mode |
--usage |
Display brief usage |
-g group |
Override group |
-u user |
Override user |
Configuration Directives
Directive | Description |
---|---|
rotate N |
Keep N old logs |
size size |
Rotate if bigger |
create mode owner group |
File creation attributes |
compress |
Compress old versions |
delaycompress |
Postpone compression |
notifempty |
Don’t rotate empty files |
missingok |
Skip missing files |
copytruncate |
Copy and truncate |
dateext |
Date extension |
mail address |
Mail old versions |
Key Use Cases
- Log management
- Disk space control
- Archive maintenance
- Compliance requirements
- System maintenance
Examples with Explanations
Example 1: Basic Configuration
/var/log/messages {
rotate 7
daily
compress
delaycompress
missingok
notifempty
}
Example 2: Size-based Rotation
/var/log/large.log {
size 100M
rotate 5
compress
create 0644 root root
}
Example 3: Weekly Rotation
/var/log/weekly.log {
weekly
rotate 4
create 0640 www-data www-data
compress
}
Common Usage Patterns
Force rotation:
logrotate -f /etc/logrotate.conf
Debug config:
logrotate -d /etc/logrotate.conf
Verbose mode:
logrotate -v /etc/logrotate.conf
Security Considerations
- File permissions
- Compression safety
- Mail configuration
- Access control
- Script execution
Additional Resources
Best Practices
- Regular testing
- Size monitoring
- Compression planning
- Retention policy
- Error handling
Configuration Examples
- Daily rotation
- Size-based rotation
- Custom scripts
- Mail notification
- Compression options
Troubleshooting
- Rotation timing
- Permission issues
- Space problems
- Script errors
- Mail delivery