Use logrotate of Linux to manage logs.

Source: Internet
Author: User

Use logrotate of Linux to manage logs.

You may need to manage logs, such as compressing logs regularly or splitting logs into two files when they exceed a certain size. I recently received such a small task. Our program uses the C language and the log4cpp library for logging. However, the problem is that log4cpp does not support the function of automatic splitting when logs exceed a certain size. It can only overwrite the previous logs from the beginning, but this is obviously not what we want. After some searches, I found that the logrotate Command provided by Linux can implement this function.

This is an introduction to logrotate:

The logrotate utility is designed to simplify the administration of log files on a system which generates a lot of log files. Logrotate allows for the automatic rotation compression, removal and mailing of log files. Logrotate can be set to handle a log file daily, weekly, monthly or when the log file gets to a certain size.

To use it, there are two main points to modify: one is/etc/logrotate. conf, and the other is the file under/etc/logrotate. d.

You can log on to logrotate. conf directly defines how to process your log file, you can also at/logrotate. d/create a corresponding file for your own log to define the log processing behavior.

Here is the link to a detailed explanation of the logrotate command: http://linuxcommand.org/man_pages/logrotate8.html

The following is an example:

/var/log/news/news.crit {           monthly           rotate 2           olddir /var/log/news/old           missingok           postrotate                                     kill -HUP ‘cat /var/run/inn.pid‘           endscript           nocompress       }

Monthly: indicates that the processing is performed once a month, and daily and weekly are commonly used.

Rotate 2: indicates that up to two backups are retained, and redundant old logs are overwritten.

Olddir: defines where old logs are stored

Missingok: skip this step if the above news. crit file cannot be found

Postrotate... endscript: These and intermediate commands define what to do after rotate is executed. Generally, they are used to let the program that uses the log file know that the log is replaced and need to re-open the file.

Nocompress: indicates that old logs do not need to be compressed for storage.

Logrotate defines how to process logs, which are scheduled by crond. Crond is a scheduled scheduling software in Unix operating systems. The following text is copied from the wiki:

The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration—though its general-purpose nature makes it useful for things like connecting to the Internet and downloading email at regular intervals.

By default, logrotate runs once a day, and its script is placed under/etc/cron. daily. In addition to cron. daily, cron. weekly, cron. monthly, and cron. hourly correspond to different frequencies. You can place scripts under different folders as needed. Do not forget to use chkconfig crond on to ensure that it will always run on startup after setting everything. Then the big work is finished.


Linux logrotate manages logs and regular execution of cron

Your configuration is simple and correct.
Logrotate is used to help rotate log files. It can be executed independently and put a script in/etc/cron. daily is called every day to achieve automatic execution. There are many configuration file parameters. For details, refer to the existing/etc/logrotate. other files under d and man logrotate. For more information, see/var/lib/logrotate. the latest rotate date in status to determine whether the log file needs rotate.

Linux logrotate command management log

The logrotate task plan is not run in the background. It is checked only after a period of time. You may need to modify the configuration. I also tested it once last time.

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.