Logrotate in Linux to cut logs (Nginx,mysql)

Source: Internet
Author: User
Tags flush

Logrotate Introduction
log rotation is especially useful for log files with fixed file names, such as MySQL error log, regular query log, slow query log, and so on. Linux system has a very easy to use according to Logratate can achieve automatic rotation, this article describes its principle and usage.

Logrotate is the tool for managing log files, in CentOS systems, where commands are located in/usr/sbin/logrotate, commonly used operations such as:
-D,--debug Don ' t do anything, just test (IMPLIES-V)
-F,--force force file rotation
Note: the-d parameter does not produce a new log.

Logrotate is typically run once a day by cron. The standard profile is/etc/logrotate.conf, and the/ETC/LOGROTATE.D directory is the location where the configuration file is saved.

Logrotate Common options:
/-----------------------------------------------
Options          |        meaning
-----------------------------------------------
compress    |    All non-current versions of the compressed log file
copy    |    Copy the current log file, ignore the Create parameter
copytruncate    |    Copy the current log file, and empty the current file
daily     |    Daily Wheel log file I
dateext    |    rotation log suffix is-YYYYMMDD format
delaycompress    |    compresses all but the current and most recent versions of
missingok    |     If the log does not exist, there is no error
notifempty    |    If the log is empty, do not rotate
rotate n     |    contains n versions of the log in a rotation scheme
size=logsize    |    Rotate
-----------------------------------------------/

If the log file is larger than logsize

By default, Logrotate is deployed as a daily cronjob, and you can find a configuration file named Logrotate in the directory/etc/cron.daily. So it runs at the top of every day? Open the file/etc/crontab know, here is the situation on my machine:

The code is as follows Copy Code

Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root
home=/

# Run-parts
* * * * Root run-parts/etc/cron.hourly
4 * * * Root run-parts/etc/cron.daily
4 * * 0 root run-parts/etc/cron.weekly
4 1 * * Root run-parts/etc/cron.monthly

From the above configuration we can know that the/etc/cron.daily is executed at 4:02 every day. That is, every day 4:02/etc/cron.daily/logrotate will be automatically executed, the following is its content:

The code is as follows Copy Code

#!/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

From the above we can see that the default profile for Logratate is/etc/logrotate.conf, and here's what it does:

The code is as follows Copy Code

# "Man logrotate" for details
# Rotate log Files Weekly
Weekly

# Keep 4 weeks worth of backlogs
Rotate 4

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

# Use date as a suffix of the rotated file
Dateext

# 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 and btmp--we'll rotate them here
/var/log/wtmp {
Monthly
Create 0664 Root utmp
MinSize 1M
Rotate 1
}

/var/log/btmp {
Missingok
Monthly
Create 0600 Root utmp
Rotate 1
}

# System-specific logs may also is configured here.

As you can see from the/etc/logrotate.conf configuration, this default configuration file will read the directory/ETC/LOGROTATE.D, so we simply put our own configuration file into the/ETC/LOGROTATE.D directory.

MySQL Configuration Chapter

MySQL This province provides a rotate reference profile, in the MySQL installation directory in the Support-files directory, the file name is Mysql-log-rotate, which reads as follows:

The code is as follows Copy Code

# This logname can is set in/etc/my.cnf
# by setting the variable ' err-log '
# in ' [Safe_mysqld] section as follows:
#
# [safe_mysqld]
# err-log=/usr/local/mysql/data/mysqld.log
#
# If The root user has a PASSW Ord you have to create a
#/root/.my.cnf configuration file with the following
# content:
#
# [Mysqladmin]
# password = <secret>
# user= Root
#
# where ' <secret> ' is the password.
#
# attentio N:THIS/ROOT/.MY.CNF should be readable only
# for Root!

/usr/local/mysql/data/mysqld.log {
        # create MySQL MySQL
        notifempty
        Daily
        Rotate 3
        Missingok
        Compress
    postrotate
         # Just if Mysqld is really running
        if Test-x/usr/local /mysql/bin/mysqladmin &&
          /usr/local/ Mysql/bin/mysqladmin Ping &>/dev/null
        then
           /usr/local/mysql/bin/mysqladmin flush-logs
         fi
    endscript
}

From the above annotation information you can see the steps:
1. Create MySQL root password file
Vi/root/.my.cnf
Edit the following content

The code is as follows Copy Code
[Mysqladmin]
Password = <secret>
user= Root

Note: <secret> is your root password
2. Permission to read root
chmod 600/root/.my.cnf
3. Copy the mysql-log-rotate to the/ETC/LOGROTATE.D directory and modify the contents to read:

The code is as follows Copy Code
/usr/local/mysql/data/*.log {
Create MySQL MySQL
Notifempty
Daily
Rotate 7
Missingok
Compress
Postrotate
# http://www.111cn.net # Just if Mysqld is really running
If Test-x/usr/local/mysql/bin/mysqladmin &&
/usr/local/mysql/bin/mysqladmin Ping &>/dev/null
Then
/usr/local/mysql/bin/mysqladmin Flush-logs
Fi
Endscript
}

4. Perform the following command test

The code is as follows Copy Code

/usr/sbin/logrotate-f/etc/logrotate.d/mysql-log-rotate

Nginx Configuration Chapter
Write the following code to/etc/logrotate.d/nginx

  code is as follows copy code
/usr/local/ Nginx/log/*.log {
        create www www.
         notifempty
        daily
         Rotate 7
        Missingok
         Compress
    postrotate
        # Servic nginx Reload
    endscript
}
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.