Logrotate is a powerful utility designed to manage and automate the rotation, compression, removal, and mailing of log files. This comprehensive guide covers all essential aspects of logrotate to help you effectively manage system logs.
- Basic Concepts
- Configuration Files
- Global Options
- Log File Definition Options
- Scripts Execution
- Compression Options
- Date Extension Options
- Include Directive
- Debugging and Testing
- Force Rotation
- State File
- Common Pitfalls
- Examples
- Best Practices
- References
- Rotation: Renaming a log file and creating a new one.
- Compression: Reducing the size of rotated log files.
- Retention: Keeping a specified number of old log files.
- Scripts: Commands executed before or after rotation.
-
Main Configuration File:
/etc/logrotate.conf
-
Additional Configuration: Files in
/etc/logrotate.d/
are included by default. -
Syntax:
<path/to/logfile> { options }
Global options are set outside of any log file definition and apply to all logs unless overridden.
- frequency:
daily
weekly
monthly
yearly
- rotate <count>: Number of times to rotate before deletion.
- compress: Compresses logs after rotation.
- nocompress: Does not compress logs.
- delaycompress: Compress logs on the next rotation cycle.
- create [mode owner group]: Creates a new log file with specified permissions.
- nocreate: Does not create a new log file after rotation.
- mail <address>: Emails the log file to the specified address.
- nomail: Does not email the log file.
- ifempty: Rotates the log file even if it's empty.
- notifempty: Does not rotate empty log files.
- missingok: Ignores missing log files without error.
- sharedscripts: Runs scripts once per group of logs.
- postrotate/endscript: Commands to execute after rotation.
These options are specified within a log file's braces {}
and override global options.
- size <size>: Rotates if log file reaches specified size.
- Units: bytes (default),
k
(kilobytes),M
(megabytes),G
(gigabytes).
- Units: bytes (default),
- minsize <size>: Minimum size to trigger rotation.
- maxsize <size>: Maximum size before forceful rotation.
- maxage <days>: Removes logs older than specified days.
- olddir <directory>: Moves rotated logs to specified directory.
- su <user group>: Rotates logs using specified user and group.
- copytruncate: Copies and truncates the original log file.
- nocopytruncate: Does not truncate the original log file.
- copy: Copies the log file instead of moving/truncating.
- nocopy: Default behavior; does not copy the log file.
Scripts can be executed at different stages:
- prerotate/endscript: Executes before rotation.
- postrotate/endscript: Executes after rotation.
- firstaction/endscript: Executes once before any logs are rotated.
- lastaction/endscript: Executes once after all logs are rotated.
Example:
/var/log/example.log {
weekly
rotate 4
postrotate
systemctl reload example.service
endscript
}
- compress: Compresses rotated logs using
gzip
by default. - nocompress: Does not compress rotated logs.
- compresscmd <command>: Specifies a custom compression command.
- uncompresscmd <command>: Specifies a custom decompression command.
- compressoptions <options>: Options passed to the compression command.
- compressext <extension>: Sets custom extension for compressed files.
- dateext: Appends the date to rotated logs.
- dateformat <format>: Custom date format (used with
dateext
).- Format Specifiers:
%Y
: Year (e.g., 2024)%m
: Month (01..12)%d
: Day (01..31)%s
: Seconds since epoch
- Format Specifiers:
Example:
dateext
dateformat -%Y-%m-%d
- include <file or directory>: Includes additional configuration files.
Example in /etc/logrotate.conf
:
include /etc/logrotate.d
-
Dry Run: Use
-d
to simulate actions without making changes.logrotate -d /etc/logrotate.conf
-
Verbose Mode: Use
-v
for detailed output.logrotate -v /etc/logrotate.conf
-
Check Configuration: Use
-f
to force rotation and test configurations.
-
Use
-f
to force rotation regardless of rotation schedule.sudo logrotate -f /etc/logrotate.conf
-
Default State File:
/var/lib/logrotate/status
-
Contains timestamps of last rotation.
-
Custom State File: Use
-s
to specify a different state file.logrotate -s /path/to/statefile /etc/logrotate.conf
- Permissions: Ensure
logrotate
has appropriate permissions. - Service Notification: Some services need to be reloaded after rotation.
- Copytruncate vs. Create: Choose based on application logging behavior.
- Script Errors: Verify scripts in
prerotate
andpostrotate
blocks.
/var/log/messages {
rotate 5
weekly
postrotate
systemctl reload rsyslog
endscript
}
/var/log/secure {
rotate 4
weekly
compress
missingok
notifempty
}
/var/log/wtmp {
monthly
create 0664 root utmp
rotate 1
delaycompress
}
/var/log/myapp.log {
size 50M
rotate 7
compress
}
/var/log/httpd/*.log {
daily
rotate 14
missingok
notifempty
sharedscripts
postrotate
systemctl reload httpd
endscript
}
- Test Configurations: Always use
-d
option before applying changes. - Monitor Disk Space: Regularly check log sizes and disk usage.
- Service Compatibility: Ensure services support log rotation mechanisms.
- Consistent Naming: Use date extensions for easier log management.
- Security: Secure log files with appropriate permissions.
-
Manual Page: Access the logrotate manual.
man logrotate
-
Online Resources:
Note: This cheatsheet provides a comprehensive overview of logrotate functionalities up to October 2023. Always refer to the latest documentation or man
pages for the most current information.
Prompt (to 4o): explain size vs maxsize vs minsize. they all sound the same
Great question! These three options in logrotate—
size
,maxsize
, andminsize
—do sound similar but serve different purposes. Here's how each works:1.
size
daily
,weekly
, etc.).Example:
myapp.log
will be rotated when it reaches 50MB, even if theweekly
ordaily
condition hasn't been met.2.
maxsize
daily
,weekly
).size
.Example:
weekly
cycle is triggered, not before.3.
minsize
minsize
at the time of the scheduled rotation, it won’t be rotated.Example:
myapp.log
every week, but it will only rotate it if the log file is at least 10MB. If it’s smaller, the log won’t be rotated until it meets the size requirement.Summary of Differences:
size
: Rotates the log immediately once it reaches the specified size, ignoring the time-based rotation schedule.maxsize
: Limits the size of the log file at the scheduled rotation. The log will be rotated during the next time-based cycle if it exceeds the specifiedmaxsize
.minsize
: Ensures the log is rotated only if it’s larger than the specified size when the rotation time arrives.Each option serves different use cases based on whether you want immediate action (
size
), control the upper limit of log files (maxsize
), or enforce a minimum size for rotation (minsize
).