Logrotate Notes Revision as of Sunday, 20 December 2015 at 19:56 UTC
This is a quick overview of logrotate
which… umm… rotates log
files. This is specific to CentOS/RHEL and was written for release 5.5.
Pertinent Files & Directories
File/Directory |
Purpose |------- |
|
The command itself. |------- |
|
Bash script that executes the |
|
Rotation defaults if they are not defined for specific daemons. You can add rotate configs to this file, or put them in |
|
Show when the specified log files were last rotated. |------- |
---|
Logrotate Options
Quite simple, really. The man
page elucidates all available options.
Here’s a quick table of what the most commonly used ones do.
Option |
Purpose |------- |
|
Keep specified number of logfiles before they are deleted and/or emailed to admin |------- |
|
Rotate logs when they reach this size.
|------- |
|
Rotate daily. This is the default minimum granularity, unless you move |
|
Rotate weekly |------ |
|
Rotate monthly |------ |
|
Don't rotate if empty (the reverse, |
|
If log file is missing for some reason, move on without error |------ |
|
Compress logfile after rotation.
|------ |
|
Use YYYYMMDD format instead of just tacking on numbers (like |
|
Email recipient the file that will be deleted after a rotation cycle |------ |
|
Create logfiles with the specified permissions and owner:group attributes. |------ |
|
Move all but the newest log file to this directory (nice to keep things organized) |
---|
Other (kinda important) options
You’ll see these used with Apache, for instance.
Option |
Purpose |------- |
|
Execute the a script before rotating a log. Should end this with |
|
Execute the a script after rotating a log. Should end this with |
|
Make sure that the script(s) specified in |
|
Don't compress yesterday's logfile (if daily... you get the picture) |
---|
Example
Here’s a real-world application of the above. I want to rotate an
already huge log of OpenVPN connections (~32M in size) like so:
- I want to maintain 60 days worth of logs
- Logfile size doesn’t matter to me
- The files should be rotated in YYYYMMDD format
- They should be compressed (the default
gzip -9
is fine) - They should be organized; older logfiles should be in a
separate directory
The logfile is at /var/log/openvpn/connections.log
. Here’s the
configuration file I created for the above. It’s
/etc/logrotate.d/openvpn
:
/var/log/openvpn/connections.log {
daily
rotate 60
dateext
compress
olddir /var/log/openvpn/old
nomail
missingok
notifempty
delaycompress
create 640 root root
}
To test this, I run:
logrotate -d /etc/logrotate.d/openvpn
The -d
switch runs it in verbose, debug, dry-run mode (i.e. nothing
actually happens.)
Other Stuff
Leopard and Snow Leopard
These use a new utility called newsyslog
. It is invoked every minute
by:
/System/Library/LaunchDaemons/com.apple.newsyslog.plist
The config file for this is /etc/newsyslog.conf
and is kinda nicer
than logrotate
’s config :)