Home » Linux » Rotating files from RAM to storage for log rotation in Linux

Rotating files from RAM to storage for log rotation in Linux

Last updated May 17, 2021

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.

Related Posts

Creating a lame utility HTTP server in Linux Operating System

In this article, we will discuss the cURL tool in Linux. The cURL tool is used for transferring the data from or to a server. It supports many protocols, and http is one of them. cURL is used to transfer the data from URL. It has so many tricks to offer, such as http...

Finding binary dependencies in Linux Operating System

In this article, we are going to check the executable. We will find out which string is present in it by using the string command.PrerequisitesBesides having a terminal open, make sure you have a binary present in your directory.Find dependencies First, we...

Capturing network traffic headlessly in Linux Operating System

In this article, we are going to learn how to capture traffic. We are going to capture network traffic with a packet sniffer tool called tcpdump. This tool is used to filter or capture TCP/IP packets that are transferred or received over a network.PrerequisitesBesides...

0 Comments

0 Comments

Submit a Comment

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

two × five =

Follow Us

Our Communities

More on Linux

The Ultimate Managed Hosting Platform
Load WordPress Sites in as fast as 37ms!