Use logrotate in Linux to manage log files cyclically
Source: Internet
Author: User
The Syslog-ng service is an important Log service in Linux systems. it must be familiar with the log management service in Linux. Now let's talk about how to manage log files. The Syslog-ng service only receives and reprocesses logs (continue to distribute or store logs...
The Syslog-ng service is an important Log service in Linux systems. it must be familiar with the log management service in Linux. Now let's talk about how to manage log files.
The Syslog-ng service only receives and reprocesses logs (continue distribution or storage), but does not manage the generated log files, the consequence is that this file is getting bigger and bigger. in our project, because we forgot to manage the log file, the log file is more than 20 GB ,? Why? Check the low tide of zookeeper Jia Yi !? Br/>
The logrotate module is responsible for log file management.
For details about logrotate, refer to the official website. The following three methods are commonly used:
Logrotate/etc/logrotate. conf: re-read the configuration file and rotate the file that meets the conditions.
Logrotate-d/etc/logrotate. conf: Debug Mode. the debugging result is output but not executed.
Logrotate-f/etc/logrotate. conf: In Force mode, rotate all related files.
The rules to be executed when logrotate manages files can be found in/etc/logrotate. create a file in the d folder, and logrotate will automatically read the corresponding rules during execution, for example, in/etc/logrotate. d/path. a new file named syslog is created. the content of the file is:
Shell code
/Log/test. log
{
Size 10 M
Create
Start 10
Rotate 4
Compress
Copytruncate
}
The code above indicates that when there are more than four files/log/logs, the first one begins to loop and iterate in turn.
In this way, the logrotate rule configuration is relatively flexible. when I need to modify the rules and add other new rules, I do not have to modify the rules in the syslog file, you can create a new file and add corresponding rules. This flexible configuration method is a great convenience for programming, and it is easier for programming to implement automatic control of rules.
Another problem is how to regularly execute the logrotate rule after it is configured? This can be controlled by crontab in Linux. for details about crontab configuration, refer to the official website. here is my demo:
Run: vi/etc/crontab
The configuration is as follows:
Shell code
*/5 * root/sbin/logrotate/etc/logrotate. conf
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.