Log files keep track of events on the system. They are essential for debugging problems and monitoring live machines. Log files grow as time passes and more events are recorded. Since the older data is less useful than the current data, log files are renamed when they reach a size limit and the oldest files are deleted.
The logrotate tool allows you to rotate the logs that are generated by applications and scripts. It keeps your log directories clutter-free and minimizes disk usage when correctly configured. logrotate is installed by default, but if it is not installed, you can install it by running the below command:
# yum install -y logrotate
logrotate Command Options
Option | Description |
---|---|
-d, –debug | Turns on debug mode and implies -v. |
-f, –force | Tells logrotate to force the rotation, even if it doesn’t think this is necessary. |
-m, –mail [command] | Tells logrotate which command to use when mailing logs. |
-s, –state | Tells logrotate to use an alternate state file. |
–usage | Prints a short usage message. |
–?, –help | -Prints help message. |
-v, –verbose | Turns on verbose mode. |
If you encounter the below error while running the command logrotate:
logrotate: command not found
you may try installing the below package as per your choice of distribution:
Distribution | Command |
---|---|
Debian | apt-get install logrotate |
Ubuntu | apt-get install logrotate |
Alpine | apk add logrotate |
Arch Linux | pacman -S logrotate |
Kali Linux | apt-get install logrotate |
CentOS | yum install logrotate |
Fedora | dnf install logrotate |
OS X | brew install logrotate |
Raspbian | apt-get install logrotate |
logrotate Command Examples
1. Trigger a run manually:
# logrotate path/to/logrotate.conf --force
2. Run using a specific command to mail reports:
# logrotate path/to/logrotate.conf --mail /usr/bin/mail_command
3. Run without using a state (lock) file:
# logrotate path/to/logrotate.conf --state /dev/null
4. Run and skip the state (lock) file check:
# logrotate path/to/logrotate.conf --skip-state-lock
5. Tell `logrotate` to log verbose output into the log file:
# logrotate path/to/logrotate.conf --log path/to/log_file