Logrotate Log rotation operation combing under Linux

Source: Internet
Author: User
Tags system log

Log files are an extremely important tool for Linux system security. the Logrotate program is a log file management tool. Used to split the log file, delete the old log file, and create a new log file that acts as a "dump". You can save disk space.
The following is a logrotate log rotation operation to do a carding record:

1) Introduction of configuration Files
The Linux system installs the Logrotate tool by default and it defaults to the configuration file in:
/etc/logrotate.conf
/etc/logrotate.d/

Logrotate.conf is the main configuration file, LOGROTATE.D is a directory, all the files in the directory are actively read into the/etc/logrotate.conf execution.
In addition, if there are no details in the file in/etc/logrotate.d/, the default value will be set to/etc/logrotate.conf the file.

Logrotate log cutting is done automatically, if you want to manually cut, you need to add-f parameters, such as:
#/usr/sbin/logrotate-f/etc/logrotate.conf
#/usr/sbin/logrotate-f/etc/logrotate.d/nginx

Logrotate command format:
Logrotate [OPTION ...] <configfile>
-D,--debug:debug mode, test the configuration file for errors.
-F,--force: Forced dump file.
-M,--mail=command: After compressing the log, send the log to the specified mailbox.
-S,--state=statefile: Use the specified state file.
-V,--verbose: Displays the dump process.

2) Cutting Introduction
For example, the system log/var/log/message to do the cutting to explain briefly:
After the first execution of the rotate (rotation), the original messages will become Messages.1, and will create an empty messages to the system to store the log;
after the second execution , Messages.1 will become messages.2, and messages will become messages.1, and create an empty messages to store the log!
If only three logs are reserved (that is, rotated 2 times), then the third time, the messages.3 file will be deleted and replaced by the newer save log later! That is, the most recent logs are saved.
The log is rotated several times, this is determined according to the Dateext parameter in the configuration file.

Look at the logrotate.conf configuration:
# cat/etc/logrotate.conf
# Under the settings is "logrotate default", if the other file set the other value,
# will be the other file settings mainly
Weekly          //rotate rotate every week by default
Rotate 4       //How many log files are reserved. The default is four. (Keep n files, that is, rotate n-1 times)
Create         //automatically creates a new log file. The new log file has the same permissions as the original file, because the log is renamed, so you want to create a new one to continue storing the previous log
dateext      //This parameter is important! That is, the cut log file ends in the current date format, such as xxx.log-20131216, If commented out, cut out by the number, that is, xxx.log-1 this format.
compress      / /whether the post-dump log file, such as xxx.log-20131216.gz, is compressed by gzip, and if no compression is required, comment out the line

Include/etc/logrotate.d
# load all the files in the/etc/logrotate.d/directory

/var/log/wtmp { //parameters set only for/var/log/wtmp
Monthly //monthly cut to replace the default one week
MinSize 1M //File size is more than 1M before cutting
Create 0664 root utmp //Specify new log file permissions and the owning users and groups
Rotate 1 //Only one log is reserved.
}
# This wtmp can record the time of user login system and system restart
# because there are minsize parameters, it is not necessarily done every month. To see the file size.

By the setting of this file can know that/ETC/LOGROTATE.D is actually the directory that is planned by/etc/logrotate.conf, although all the configuration can be written to/etc/logrotate.conf, But this file is really too complex, especially when using a lot of services on the system, each service to modify the/etc/logrotate.conf setting also seems to be unreasonable.
So, if you separate a directory, then each service to cut the log, you can become a file alone, and placed in the/etc/logrotate.d/

Logrotate Log rotation operation combing under Linux

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.