logrotate Command Examples in Linux

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.

logrotate Command Examples

1. To force the log rotation:

# logrotate -f /etc/logrotate.conf
# logrotate --force /etc/logrotate.conf 

2. To test the log rotation:

# logrotate -d /etc/logrotate.conf
# logrotate --debug /etc/logrotate.conf 

3. To set to verbose mode:

# logrotate -v /etc/logrotate.conf
# logrotate --verbose /etc/logrotate.conf 

4. To get the help for logrotate:

# logrotate -?
# logrotate --help 

5. To display the brief usage help:

# logrotate --usage 

Final Thoughts

The logrotate tool is a script that is launched by cron everyday. The directives added to the default logrotate definition are compressed, daily, delaycompress, dateext, missingok, and rotate.

Related Post