CentOS Linux uses logrotate to split management logs

Source: Internet
Author: User

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.


Logrotate command format:

Logrotate [OPTION ...] <configfile>

-D,--debug:debug mode, test the configuration file for errors.

-F,--force: Forced dump file.

-M,--mail=command: Sends the log to the specified mailbox.

-S,--state=statefile: Use the specified state file.

-V,--verbose: Displays the dump process.


The configuration file for Logrotate is/etc/logrotate.conf. To view the default configuration conditions:

Cat/etc/logrotate.conf

Shown below:


# see ' Man logrotate ' for details

# Rotate log Files Weekly

Weekly

# Keep 4 weeks worth of backlogs

Rotate 4

# Create new (empty) log files after rotating old ones

Create

# Uncomment this if you want your log files compressed

#compress

# RPM Packages Drop log rotation information into this directory

Include/etc/logrotate.d

# no packages own wtmp--we'll rotate them here

/var/log/wtmp {

Monthly

MinSize 1M

Create 0664 Root utmp

Rotate 1

}

# system-specific logs May is also is configured here.


Brief description:

Weekly: All log files are dumped once a week.

Rotate 4: The dump file is divided into 4 parts.

Create:logrotate automatically creates a new log file.

Compress: Compresses the log file. The default is commented out.

INCLUDE/ETC/LOGROTATE.D: Read into the log dump parameter in the/ETC/LOGROTATE.D directory, when the RPM package is installed in the system, the log dump parameters of the RPM packet are usually automatically established in the/ETC/LOGROTATE.D directory.

/VAR/LOG/WTMP segment: Configuration of the/var/log/wtmp log dump.


Monthly: The log file will be round by month. Other available values are ' daily ', ' weekly ' or ' yearly '.

Rotate 5:5 archive logs will be stored at a time. For a sixth archive, the oldest archive will be deleted.

Compress: After a round-robin task is completed, the archived archive will be compressed using gzip.

Delaycompress: Always with the Compress option, the Delaycompress option indicates that logrotate does not compress the most recent archive, and compression will take place during the next round-robin cycle. This is useful when you or any software still needs to read the latest archive.

Missingok: During log rotation, any errors will be ignored, such as "files cannot be found".

Notifempty: If the log file is empty, the round robin does not proceed.

Create 644 root root: Creates a completely new log file with the specified permissions, and logrotate renames the original log file.

Postrotate/endscript: After all other instructions have been completed, the commands specified inside postrotate and Endscript will be executed. In this case, the RSYSLOGD process will immediately read its configuration again and continue to run


Use Logrotate management Lnmp to install the connection log for Nginx in the package, lnmp the log file in the/home/wwwlogs directory.

To create a configuration file:


Template One

/var/log/log-file {

Monthly

Rotate 5

Compress

Delaycompress

Missingok

Notifempty

Create 644 root root

Postrotate

/usr/bin/killall-hup RSYSLOGD

Endscript

}

Template Two

Vim/etc/logrotate.d/nginx

Enter as follows:

/home/wwwlogs/access.log/home/wwwlogs/nginx_error.log {

Notifempty

Daily

Rotate 5

Sharedscripts

Postrotate

/bin/kill-hup '/bin/cat/usr/local/nginx/logs/nginx.pid '

Endscript

}

Template three (we want to have the old log file named after the creation date, which can be achieved by adding Dateext Changshu. )

/var/log/log-file {

Monthly

Rotate 5

Dateext

Create 644 root root

Postrotate

/usr/bin/killall-hup RSYSLOGD

Endscript


Description

Notifempty: If it is an empty file, do not dump it.

Daily: Log files are dumped once a day.

Rotate 5; The dump file is divided into 5 parts.

Postrotate/endscript: script executed after the log dump. This is used to let Nginx regenerate the log file. The nginx.pid is the main process number of nginx.


Execution Logrotate:

/usr/sbin/logrotate-vf/etc/logrotate.conf


If there is no error, generated a dump file, Nginx normal access, OK.


How the logrotate automatically executes:

There are scripts executed by logrotate in the/etc/cron.daily directory. Executed once a day through the crontab program.


When you are finished configuring the call

logrotate/etc/logrotate.conf or specify Logrotate/etc/logrotate.d/http


This article is from the "It po" blog, please be sure to keep this source http://907832555.blog.51cto.com/4033334/1965283

CentOS Linux uses logrotate to split management logs

Related Article

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.