What is Logrotate and Its Uses

Developing programmer Development Website design and coding technologies working
Reading Time: 3 minutes

Before starting the logrotate we should know about what are logs.

  • Logs are the records of everything and anything that happens within a system.
  • It is helpful for finding what an application is doing and troubleshooting for a possible problem.
  • Linux has a directory for storing logs i.e /var/log. This directory contains logs of OS ,services and applications that run on the system.

What is logrotate ?

  • It is a command-line tool in linux for log managing.
  • The main purpose of log rotation is to restrict the volume of the log data to avoid overflow the record store.
  • While keeping the logs in a single files it will store the logs and make a big amount file and it is difficult for troubleshooting. For solve this problem we can use logrotate.
  • In logrotate we rotate the logs file on hourly,daily,monthly and yearly basis according to the need.

Working of logrotate 

  • When a log file reaches a specific size it will rotate.
  • Compress and the rotated log files
  • Rotate the old log files with the date or name in the filename.
  • We can run custom shell script after the log rotation.

Format for Writing Logrotate

<file path matchers 1> {

    <directive 1>

    <directive 2>

    <directive n>


Example :

/var/log/test/*.log {
	rotate 14
        dateformat  -%d-%m-%Y

In this example /var/log/test/*.log is the file path.

Here all files with .log extension will rotate on daily basis because the rotation period is defined in .conf file is daily basis.

There i am using some directive like missingok , rotate 14 ,compress , notifempty, create, dateformat etc.

So the missingok means if in the test folder any file with .log extension not found this will not give any error message.

rotate 14 means it will kept 14 Old log files.

notifempty means , if the log file is empty then it will not rotate.

for more details about directives you can go to the man logrotate command.

Steps how to use logrotate:

  • In the below image it is content of service file here i am running a python script as a background service and run script on 15 Sec interval.Here the directive StandardOutput and StandardError store the logs on the /home/knoldus/test1 path with file name Outputtest.log and Errortest.log.

  • In below image i have created test.conf file.
  • In this conf file i am rotate the log file in Hourly basis and compress the files after completing the required condition.
  • Here in the .conf file dateformat directive used it means when a log file rotate on particular time then the compress file name saved with filename and date format that mentioned in .conf file
  • It will show like this Outputtest.log-2022-09-23_07:01:01.gz.

  • After writing the config file can set the cronjob , for that we type the crontab -e command in terminal and set the cronjob according to your need.
  • For running the cronjob on every hour we pass this command , 1 * * * * /usr/sbin/logrotate /home/knoldus/test.conf –state /home/knoldus/logrotate-state.

As you can see in the below image ,The test1 directory has all the compressed files that rotate on hourly basis.

The Errortest.log-2022-09-22_07:01:01.gz stores the logs of 1 hour.

Written by 

Harshit Gupta is a Software Consultant at knoldus Inc having few year experience in DevOps . He always eager to learn new technology and helping to others .