Manage logs with logrotate from Linux

Source: Internet
Author: User

Logrotate is controlled by Cron and Cron executes the "logrotate/etc/logrotate.conf" command at the specified time. The object log is dumped, deleted, compressed and other operations ...

This is an official introduction to the Logrotate Log rotation tool:

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

In order to use it, there are two main places to modify: One is/etc/logrotate.conf, the other is the/etc/logrotate.d/file below.

You can define how to handle your log file directly in logrotate.conf, or you can create a new file for your own log under/logrotate.d/to define the behavior of handling log.

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

Here is an example of a timed cut compression for Nginx logs: (catalina.out logs under Tomcat similarly )

# Cat/etc/logrotate.d/nginx

/data/logs/nginx/*.log {    Rotate 365    copytruncate    create    Missingok    notifempty
Daily sharedscripts compress postrotate [-f/var/run/nginx.pid ] && kill-usr1 ' CAT/ Var/run/nginx.pid ' Endscript}

Monthly: The description is a month to deal with, often also have daily,weekly. When you add daily, the log format becomes *.log-20170313.gz

Rotate 365: It means that at Most 365 backups are kept on rotation, and the extra old logs are overwritten. The most recent generation is always *.1.gz, pushing backward and overwriting

Copytruncate: The role of a copy of the current log file for processing, and then empty the source log file, so that it continues to receive logs. (Empty but not delete log files)

Create: After processing the log file, the freshmen into a log file, of course, as far as possible with the same name and permissions, etc.

Olddir: Defines where the old log is stored

Missingok: Meaning if the above *.log file can not be found, do not error, skip directly

Notifempty: If the log file is empty, no rotate rotation is done

Sharedscripts: Corresponds to Endscript, middle-put script

Prerotate: a command or script executed before starting logrotate, such as modifying the properties of a file or directory, etc.

Postrotate: a command or script executed after starting logrotate, such as to reload a service into a configuration file, reopen the log file (Kill-hup or KILL-USR1)

Here to say that the difference between Kill-hup and KILL-USR1 is only that kill-hup will reset the process, the equivalent of/usr/local/nginx/sbin/nginx-s reload, and KILL-USR1 execution of the first child process number is unchanged

Nocompress: Indicates that the old log does not need to be compressed to save

MinSize 1G: Log minimum 1G, less than 1G not rotation

Logrotate defines how the log is handled, which itself is called by the Crond timing. Crond is a Unix-based scheduling software in the operating system, 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 periodic Ally at fixed times, dates, or intervals. It typically automates system maintenance or administration-though its general-purpose nature makes it useful for things L Ike connecting to the Internet and downloading e-mail at regular intervals.

The default logrotate is to run once a day, and its script is placed in /etc/cron.daily/below. In addition to cron.daily and cron.weekly,cron.monthly,cron.hourly, etc., you can put the script under different folders depending on your needs. After you've set everything out, don't forget to use chkconfig crond on to keep it up and running. And then the Gaocheng.

# cat/etc/cron.daily/logrotate (default)

#!/bin/sh/usr/sbin/logrotate/etc/logrotate.conf >/dev/null 2>&1exitvalue=$?if [$EXITVALUE! = 0]; Then    /usr/bin/logger-t logrotate "ALERT exited abnormally with [$EXITVALUE]" Fiexit 0

If you want to customize the frequency of execution time, you can customize the scheduled tasks, for example: (once per hour)

0 * * * * root/usr/sbin/logrotate- f /root/nginx-f,--force Force file rotation

View the specific implementation of each log file rotate:

Cat/var/lib/logrotate.status

Reference: http://www.cnblogs.com/tiantiandas/p/rsyslog.html http://www.cnblogs.com/futeng/p/4785206.html reprint Yu Tianyu Knight

Manage logs with logrotate 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.