Logrotate tool usage summary in Linux system

  

Linux system operation, Logrotate is a log management tool, can handle Linux logs, you need to configure the Logrotate tool before use, the following small series will introduce you The usage of the Logrotate tool in Linux, let's get to know it.

1 running principle

Logrotate is based on the CRON to run, the script is /etc/cron.daily/logrotate

#! /bin/sh

/usr/sbin/logrotate /etc/logrotate.conf

EXITVALUE=$?

if [ $EXITVALUE ! = 0 ]; then

/usr/bin/logger -t logrotate “ALERT exited abnormally with [$EXITVALUE]”

fi

exit 0

The /etc/cron.daily directory of all scripts is run by CRON via /etc/crontab.

There are two ways to run: 1 Directly execute a single command; 2 Directory planning, just list the directories below Planning configuration

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

HOME=/

# run-parts

*/1 * * * * root run-parts /etc/cron.min

01 * * * * root Run-parts /etc/cron.hourly

59 23 * * * root run-parts /etc/cron.daily

22 4 * * 0 root run-parts /etc/cron. Weekly

42 4 1 * * root run-parts /etc/cron.monthly

2 configuration files

/etc/logrotate.conf global default file

/etc/logrotate.d/directory, subordinate files are included in the former by include

Common Options

weekly <<= = The default is to execute the rotate once a week

rotate 4 <<== How many log files are kept. Four are reserved by default.

create "==Create a new file. Because the log is renamed, create a new one to continue storing the previous log

dateext <<==The file suffix is ​​the date format, that is, the file after cutting is: xxx.log-20131216, if commented out, Cut out is incremented by number, that is, xxx.log-1

compress <<== whether to compress the log.

include /etc/logrotate.d # Load all the files in the /etc/logrotate.d/directory

/var/log/wtmp { <<==only for /var /log/wtmp Set parameters

monthly <<= once a month to cut, replace the default week

minsize 1M <<==file size exceeds 1M will be cut

create 0664 root utmp <<==Specify newly created log file permissions and associated users and groups

rotate 1 <<= Only one log is kept.

}

Examples of files loaded by include are as follows

[root@www ~]# vi /etc/logrotate.d/syslog

/var /log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron

{

sharedscripts

prerotate

/usr/bin/chattr -a /var/log/messages

endscript

sharedscripts

postrotate

/bin/kill -HUP `cat /var/run/syslogd.pid 2 " /dev/null` 2 " /dev/null | |  True

/bin/kill -HUP `cat /var/run/rsyslogd.pid 2 " /dev/null` 2 " /dev/null | |  True

/usr/bin/chattr +a /var/log/messages

endscript

}

Log file: absolute path to the log being processed . Use a space character to separate multiple file names;

Execute script:

You can call external commands for additional commands. This setting needs to be sharedscripts. . . . Endscript is set to be used together. Command introduction:

prerotate: instructions that are executed before starting logrotate, such as modifying the properties of the file;

postrotate: instructions that are started after logrotate is completed, such as restarting (kill - HUP) A service;

Then the cutting function of the six files set in /etc/logrotate.d/syslog becomes:

1. This setting is only for /Messages, secure, maillog, spooler, boot.log, cron in var/log/are valid;

2. Log cutting once a week, retaining four, and the cut log files are not compressed (not Change the default value);

3. After the completion of the post (postrotate) After obtaining the PID of the syslog, restart syslogd with kill -HUP

Cutting case: Nginx

= ============================================================================================================== /local/nginx/logs/*.log {

daily

rotate 5

dateext

compress

sharedscripts

postr Otate

if [ -f /usr/local/nginx/logs/nginx.pid ]; then

kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`

fi

endscript

}

The above is the usage of the Logrotate tool in Linux. This article describes the operation of the Logrotate tool and the Logrotate file. Configuration, if you want to manage Linux system logs, you can choose to use the Logrotate log management tool.

Copyright © Windows knowledge All Rights Reserved