Guidance in general, logging is a very important part of any troubleshooting process, but these logs will grow over time. In this case, we need to manually perform log cleanup to reclaim space, which is a tedious management task. To solve this problem, we can configure the Logrotate program in Linux to automatically rotate, compress, delete, and mail out log files. We can configure the Logrotate program so that each log file can be processed on a daily, weekly, monthly, or when it becomes too large.
How logrotate is working.
By default, the Logrotate command runs once a day as a cron task placed in/etc/cron.daily, and it helps you set up a policy in which log files that exceed a certain time or size are rotated.
Command:
/usr/sbin/logrotate
Profile:/etc/logrotate.conf, this is the logrotate primary configuration file. Logrotate also stores the configuration of specific services in/etc/logrotate.d/. Make sure that the following line is included in/etc/logrotate.conf to read the specific service log configuration.
Include /ETC/LOGROTATE.D '
Logrotate History:/var/lib/logrotate.status
Important Logrotate Options:
Compress --> All non current versions of the compressed log file
daily,weekly,monthly--> to rotate log files
delaycompress --> Compress all versions by specified schedule , in addition to the current and next most recent
endscript --> tag prerotate or postrotate script end
errors "Emailid" --> send an error notification to the specified mailbox
Missingok --> If the log file is missing, do not display an error
notifempty --> If the log file is empty, do not rotate the log file
olddir "dir" - > specify that older versions of log files are placed in "dir"
postrotate --> introduces a script that executes after the log is rotated
prerotate --> introduces a script that executes before the log is rotated
rotate ' n ' --> contains the n versions of the log in the rotation scheme
sharedscripts --> runs only one script for the entire log group
size= ' LogSize ' --> rotation When the log size is greater than logsize (for example, 100k,4m)
Configuration
Let's configure logrotate for our own sample log file/tmp/sample_output.log.
First step: Add the following line to the/etc/logrotate.conf.
/tmp/sample_output.log {
size 1k
create $ root root
rotate 4
compress
}
In the configuration file above:
Size 1k-logrotate runs only when the file size is equal to (or greater than) this size. Create-Rotates the original file and creates a new file with the specified permissions, users, and groups. Rotate-limits the number of log file rotations. Therefore, this will retain only the most recent 4-rotated log files. Compress-This will compress the file.
Step two: Usually, you need to wait a day to wait until Logrotate is executed by/etc/cron.daily. In addition, you can use the following command to run on the command line:
/usr/sbin/logrotate /etc/logrotate.conf
Output prior to executing the logrotate command:
[Root@rhel1 tmp]# ls-l/tmp/total
-rw-------. 1 root 20000 the 1 05:23 sample_output.log
Output after execution of logrotate:
[Root@rhel1 tmp]# ls-l/tmp
-rwx------. 1 root 0 before 1 05:24 sample_output.log-rw
-------. 1 ro OT root 599 1 05:24 sample_output.log-20170101.gz
[Root@rhel1 tmp]#
This will confirm that Logrotate has been successfully implemented.
This article address: http://www.linuxprobe.com/configure-logrotate-tutorial.html
Free to provide the latest Linux technology tutorials Books, for the open source technology enthusiasts to do more and better, open source site: http://www.linuxprobe.com/