Control log file size using the Logrotate program from Linux

Source: Internet
Author: User

1. Write the configuration file as follows (take the Amadeus system as an example):

Write the configuration file, put under the/etc/logrotate.d/xxxx, where xxxx is the name you take, without a suffix. For example, this file in the Amadeus system is/etc/logrotate.d/amadeus

/usr/local/tomcat_amadeus/logs/catalina.out {    copytruncate    daily    dateext         Compress    missingok    size 2000M}

The first line is the full path of the file, and the important fields in the curly braces are explained as follows:

Copytruncate use the first copy of the file and then truncate the original file method to rotate, this method does not need to stop the application, there is no data loss.
Daily performed on a daily basis
Dateext post-rotation log backup plus date suffix
Rotate 15 A total of 15 copies of backup files
Compress enable gzip compression to significantly reduce the size of log backups
Missingok log file does not exist without an error (if there are multiple configurations, will continue to execute)
Size 2000M rotate only if the file is larger than 2000M

A detailed explanation of the configuration items can refer to man logrotate.

2. Set up crontab tasks

The Logrotate program is executed by default as a system-wide daily task, which is supposed to be done automatically by simply writing the configuration file. But I found that this would be a non-working situation, so the display is configured with a crontab task to execute it, as follows:

XX * * */usr/sbin/logrotate-v/etc/logrotate.d/amadeus >>/home/jerry/logrotate.log 2>&1

Replace the configuration file path of the parameters section with your own configuration file. Note that it is important to ensure that the user running crontab has read and write access to the log files and directories in which they reside. It is recommended to use the user directly when running Tomcat.

The way to add crontab is to use the corresponding user login system, execute CRONTAB-E, add a line as above.

3. View, search log backup

Effects after Logrotate is enabled:

[Email protected] logs]$ LS-LH catalina.out*-rw-r--r--. 1 Jerry xxxxxxxx 2.2G Jan 14 14:52Catalina.out-rw-r--r--. 1 Jerry xxxxxxxx 93M Dec 00:00 catalina.out-20131230. GZ-rw-r--r--. 1 Jerry xxxxxxxx 83M Dec 00:00 catalina.out-20131231. GZ-rw-r--r--. 1 Jerry xxxxxxxx 110M Jan 1 00:00 catalina.out-20140101. GZ-rw-r--r--. 1 Jerry xxxxxxxx 165M Jan 3 00:01 catalina.out-20140103. GZ-rw-r--r--. 1 Jerry xxxxxxxx 111M Jan 4 00:01 catalina.out-20140104. GZ-rw-r--r--. 1 Jerry xxxxxxxx 110M Jan 5 00:00 catalina.out-20140105. GZ-rw-r--r--. 1 Jerry xxxxxxxx 106M Jan 6 00:00 catalina.out-20140106. GZ-rw-r--r--. 1 Jerry xxxxxxxx 167M Jan 7 00:01 catalina.out-20140107. GZ-rw-r--r--. 1 Jerry xxxxxxxx 208M Jan 8 00:01 catalina.out-20140108. GZ-rw-r--r--. 1 Jerry xxxxxxxx 170M Jan 9 00:01 catalina.out-20140109. GZ-rw-r--r--. 1 Jerry xxxxxxxx 194M Jan 00:01 catalina.out-20140110. GZ-rw-r--r--. 1 Jerry xxxxxxxx 172M Jan 00:01 catalina.out-20140111. GZ-rw-r--r--. 1 Jerry xxxxxxxx 119M Jan 00:00 catalina.out-20140112. GZ-rw-r--r--. 1 Jerry xxxxxxxx 145M Jan 00:01 catalina.out-20140113. GZ-rw-r--r--. 1 Jerry xxxxxxxx 181M Jan 00:01 catalina.out-20140114.gz

As you can see, when compression is enabled, a few g of log space per day is reduced to just over 100 m.

By this configuration, the backup log files are compressed and will have the GZ extension. When you need to query and view these files, you can use the following methods:

1. View: Using Zless
2. Find string: Use Zgrep
3. Display content: Using Zcat
4. Restore files: Gunzip xxxx.gz

The general situation is not recommended to use Gunzip decompression, but directly using tools such as Zless,zcat. For example, to grep a keyword from the old log and do the processing, you can do this:

Zgrep Exception catalina.out-20131230.gz

Or

Zcat catalina.out-20131230.gz | grep Exception

Control log file size using the Logrotate program from 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.