Linux Commands

Linux Logrotate Examples

Log information is a very important part of any operating system to diagnose the problem of any running application and the log entries help to find out the solution of the problem. Most of the applications of the Linux operating system generate the log and the system administrator gets the required information about the application from the log entries. However, the log entries sometimes create problems because the size of the entries becomes larger with time. So, the log entries are required to manage.

The logrotate is a command-line tool of Linux to manage the log entries. This tool helps to perform different types of tasks on log entries by the administrator such as limiting the rotated log files, compressing the rotated log files, deleting the unnecessary log files, executing the particular shell script based on the log files, etc. The uses of the “logrorate” command for managing the log files in different ways are shown in this tutorial using multiple examples.

Check the Installed Logrotate Version

The “logrotate” command is installed by default in the new version of the Ubuntu operating system. Run the following command to check the installed version of the “logrotate” command:

$ logrotate --version

The log entries of the different applications are stored in the “/var/log” folder by default. The following similar content will appear if you check the content of the folder.

$ ls /var/log

Set the “Logrotate” Configuration

Setting value Purpose
daily/weekly/monthly/yearly It defines the time duration to rotate the logs.
rotate number It defines the number of files that will be kept before removing the old log files.
compress It is used to compress the log files.
compresscmd It is used to set the “compress” command. The gzip is the default command.
uncompresscmd It is used to set the “uncompress” command. The gunzip is the default command.
delaycompress It is used to delay the compression process of the log files.
notifempty It is used to not rotate the empty file.
missingok If it is set, no error is generated for the missing log files.
size It is used to set the limit to start rotating the log files.
dateext It is used to add a date value as a suffix of the rotate file.
copytruncate It is used to create a copy of the original file.
prerotate It is used to run a script before rotating the log files.
postrotate It is used to run a script after rotating the log files.
create It is used to create the log files with a root privilege.

Syntax:

The syntax of the “logrotate” command is given as follows:

logrotate [OPTION] config_file_path

Different types of options can be used for different purposes with the “logrotate” command.

Logrotate Options

Some useful options of the “logrotate” command are mentioned in the following:

Option Purpose
-f, –force It is used to make the rotation forcefully when required.
-d, –debug It is used to enable the debug mode during the rotation.
-m, –mail <command> It is used to send an email during the rotation.
-s, –state <statefile> It is used for alternative state files.
–usage It is used to print the usage information.
–?, –help It is used to print the helping messages.
-v, –verbose It is used to print in verbose mode.

 

Logrotate Configuration File

The main logrotate configuration file is located at the “/etc/logrotate.conf” location. Run the following command to open the file in the nano editor:

$ nano /etc/logrotate.conf

The default setting of the “logrotate” command is shown in the “logrotate.conf” file. The “include” directive is used in the file to retrieve the configuration that is located in the “/etc/logrotate.d” directory.

Example 1: Create a Simple Logrotate Configuration File

Create a sample log file named “/var/log/test.log” with the sample log data. Run the following command to open the nano editor to create a new “logrotate.conf” file in the “/etc/tmp” folder location. Create the “/tmp” folder with root privileges if it is not created before.

$ nano /etc/tmp/logrotate.conf

Add the following content to the file for the “/var/log/test.log” file. According to the setting, the “test.log” file will be rotated daily if the file size exceeds 5K:

/var/log/test.log {

  daily

  size 5K

  su root adm

}

Run the following command to check the size of the log file:

$ ls -l /var/log/test.log

Run the “logrotate” command after creating the configuration file.

$ sudo logrotate /etc/tmp/logrotate.conf

Run the following command again to check the size of the log file after executing the “logrotate” command:

$ ls -l /var/log/test.log

The size of the “test.log” file is 1K+. So, no rotation is done based on the configuration setting.

Change the size value to 1K in the “/etc/tmp/logrotate.conf” file and run the “ls” command again to check the file size of the “/var/log/test.log”. According to the output, the log file is rotated and deleted because the size limit is exceeded.

Example 2: Use of Logrotate Copytruncate

Create or modify the “/etc/tmp/logrotate.conf” file with the following settings to show the use of the copytruncate. According to new the settings, logrotate creates a copy of the original file by making the original file size to zero.

/var/log/test.log {

  rotate 5

  size 1k

  copytruncate

  su root adm

}

 

Run the following command to check the size of the “test.log” file:

$ ls -l /var/log/test.log

Run the “logrotate” command after creating the configuration file.

$ sudo logrotate /etc/tmp/logrotate.conf

Run the following command again to check the size of the “test.log” file after executing the “logrotate” command:

$ ls -l /var/log/test.log

The original file size becomes 0 after executing the “logrotate” command for the copytruncate setting.

 Example 3: Use of Logrotate Compress

Create or modify the “/etc/tmp/logrotate.conf” file with the following settings to show the use of the compress. According to new the settings, logrotate creates a compress file of the original file.

/var/log/test.log {

  rotate 5

  size 1k

  compress

  create 770 root adm

}

 

Run the following command to check the list of the files and folders of “/var/log”:

$ ls /var/log/

Run the “logrotate” command after creating the configuration file.

$ sudo logrotate /etc/tmp/logrotate.conf

Run the following command again to check the list of the files and folders of “/var/log”:

$ ls /var/log/

The compressed file of the “test.log” file is created with the name “test.log.1.gz” and the original file is removed.

Example 4: Use of Logrotate Dateext

Create or modify the “/etc/tmp/logrotate.conf” file with the following settings to show the use of dateext. According to the new settings, logrotate creates a compress file of the original file with the date value.

var/log/test.log {

  su root adm

  rotate 5

  size 1k

  compress

  create 770 root adm

  dateext
 
}

Run the “logrotate” command after creating the configuration file.

$ sudo logrotate /etc/tmp/logrotate.conf

Run the following command to check the list of the files and folders of “/var/log”:

$ ls -l /var/log/

The compressed file of the “test.log” file is created with the name “test.log.20240129.gz” and the original file is removed.

Example 5: Use of Logrotate Maxage

Create or modify the “/etc/tmp/logrotate.conf” file with the following settings to show the use of the maxage. According to the settings, logrotate keeps five log entries if the size of the log file exceeds 1K after one day.

/var/log/test.log {

  su root adm

  rotate 5

  size 1k

  compress

  maxage 1

}

Run the following “logrotate” command to store the output in another log file named “out.log”:

$ sudo logrotate -s=/var/log/out.log /etc/tmp/logrotate.conf

According to the following output, the “out.log” file is created after executing the “logrotate” command:

Example 6: Use of Logrotate Missingok

Create or modify the “/etc/tmp/logrotate.conf” file with the following settings. Here, the “testfile.log” log file does not exist in the “/var/log” folder.

/var/log/testfile.log {

  su root adm

  rotate 5

  size 1k

  compress

}

An error message is printed after executing the “logrotate” command.

Add the “missingok” setting in the logrotate configuration file and run the “logrotate” command again. No error is printed for the missing log file.

Example 7: Use of Logrotate Prerotate

Create a Bash file named “test.sh” with the following script that prints a simple message. The file is used in this logrotate example to show the use of the prerotate in the logrotate configuration file.

test.sh

 

#!/bin/bash

echo "logrotate examples..."

After creating the file, run the following command to set the execution permission of this file for all users:

$ chmod a+x /home/fahmida/test.sh

Now, create or modify the “/etc/tmp/logrotate.conf” file with the following settings. According to the settings, logrotate keeps five log entries if the size of the log file exceeds 1K and the “test.sh” file is executed before the rotation.

/var/log/test.log {

  su root adm

  rotate 5

  size 1k

  prerotate

  /home/fahmida/test.sh

  endscript

}

The output of the “test.sh” file is shown after executing the “logrotate” command:

Conclusion

The various uses of the “logrotate” command are shown in this tutorial using multiple examples that will help the Linux user to know the uses of the command and manage the log files properly.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.