Logrotate
lastmod 2025-04-17created 2015-01-159 edits
logrotate ‐ rotates, compresses, and mails system logs
Categories
- Cron:Examples:LogFileRotate - crontab example that automatically splits log files by date.
SYNOPSIS
logrotate [--force] [--debug] [--state file] [--skip-state-lock] [--verbose] [--log file] [--mail command] config_file [config_file2 ...]
DESCRIPTION
logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.
Normally, logrotate is run as a daily cron job. It will not modify a log more than once in one day unless the criterion for that log is based on the log's size and logrotate is being run more than once each day, or unless the -f or --force option is used.
Any number of config files may be given on the command line. Later config files may override the options given in earlier files, so the order in which the logrotate config files are listed is important. Normally, a single config file which includes any other config files which are needed should be used. See below for more information on how to use the include directive to accomplish this. If a directory is given on the command line, every file in that directory is used as a config file.
If no command line arguments are given, logrotate will print version and copyright information, along with a short usage summary. If any errors occur while rotating logs, logrotate will exit with non-zero status, although the state file will be updated.
About
If messages from the kernel and each daemon keep being logged, log files grow until they reach the file size limit of the file system, and messages can no longer be logged. logrotate exists to prevent this.
logrotate runs via cron, moves and compresses the specified log files, and creates new log files. It also restarts daemons if necessary.
Processing order
- cron reads
/etc/cron.daily/logrotate - logrotate runs with
/etc/logrotate.confas its config file and processes the log files specified inlogrotate.conf - Reads the files under
/etc/logrotate.d/and processes the specified log files
List of key logrotate directives
- create
- missingok
- rotate
- daily, weekly, monthly, yearly
- compress
- delaycompress
- compresscmd
- postrotate/endscript
- notifempty
- sharedscripts
- size/minsize
- nocreate
See also
Troubleshooting
Free-space driven log rotation
A logrotate config that checks the remaining disk space and removes files:
/mnt/user/logs/*.log /mnt/user/logs/*/*.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
su root www-data
create 760 root www-data
firstaction
for file in `find -type f -size +1024M`; do
percent=`df -h | grep /mnt/user | awk '{print $5}' | sed 's/%//'`
if [ $percent -gt 50 ]; then
echo "Removed $file" >> /mnt/user/logs/logrotate.log
rm $file
fi
done;
endscript
}
Favorite site
- Linux log file management (logrotate)
- Linux log management - Logrotate
- (docker) log management (docker, logrotate)
- How to setup log rotation for a Docker container (docker, logrotate)
- Using logrotate (on CentOS)
- Joinc - Managing log files with logrotate
- Linux log management - Logrotate
- (Linux) Logrotate configuration