Rotating files from RAM to storage for log rotation in Linux

In this article, we are going to discuss the logrotate Linux tool. Using this tool, administration of systems becomes easy. The systems generate large number of log files. This allows for automatic rotation, removal, compression, and mailing of log files.

We can handle each and every log file. We can handle them daily, weekly, and monthly. Using this tool, we can keep logs longer with less disk space. The default configuration file is /etc/logrotate.conf. Run the following command to see the contents of this file:

$ cat /etc/logrotate.conf

You will see the following:

/etc/logrotate.conf

weekly 
rotate 4 
create 

include /etc/logrotate.d 
# no packages own wtmp, or btmp -- we'll rotate them here 
/var/log/wtmp { 
	missingok 
	monthly 
	create 0664 root utmp 
	rotate 1 
} 

/var/log/btmp { 
	missingok 
	monthly 
	create 0660 root utmp 
	rotate 1 
} 

# system-specific logs may be configured here

Prerequisites

To use logrotator, you must be aware of the logrotate command.

How to do it

We are going to look at an example configuration.

We have two options for managing log files:

  • Create a new configuration file and store it in /etc/logrotate.d/. This configuration file will be executed daily along with other standard jobs. This will be with root privileges.
  • Create a new configuration file and execute it independently. This will execute with non-root privileges. This way, you can execute it manually, at whatever times you want.

Adding configuration to /etc/logrotate.d/

Now, we will configure a web server. This puts information like access.log and error.log into the /var/log/example-app. It will act as a data user or group.

To add some configuration to /etc/logrotate.d/, first open up a new file there:

$ sudo nano /etc/logrotate.d/example-app

Write the following code in example-app:

/var/log/example-app/*.log { 
	daily 
	missingok 
	rotate 14 
	compress 
	notifempty 
	create 0640 www-data www-data 
	sharedscripts 
	postrotate 
	systemctl reload example-app 
	endscript 
}

Some of the new configuration directives in this file are as follows:

  • create 0640 www-data www-data: After rotation, this will create a new empty log file with specified permissions for the owner and group.
  • sharedscripts: This means that configuration scripts are run only once per run instead of there being a rotation for each file.
  • postrotate to endscript: This particular block has a script that has code for running after the log file is rotated. Using this, our application can switch over to the newly created log file.

How it works

We can customize the .config file according to our needs and then save that file in /etc/logrotate.d. For that, run the following command:

$ sudo logrotate /etc/logrotate.conf --debug

After running this command, logrotate will point the standard configuration file, and then it will be on debug mode. It will give us the information about the files which logrotate is handling.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Related Articles