nginx logrotate 日誌切割

vim /etc/logrotate.d/nginx 

/home/wwwlogs/*nginx.log {
daily
rotate 5
missingok
dateext
compress
notifempty
sharedscripts
postrotate
    [ -e /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}




/var/log/mysql/mysql.log
/var/log/mysql/slow.log
{
                daily
                rotate 30
                #dateext
                compress
                missingok
                notifempty
                create 640 mysql mysql
                olddir /var/log/mysql_log_rotate
                sharedscripts
        postrotate
                # just if mysqld is really running
                if test -x /usr/local/mysql/bin/mysqladmin && \
                /usr/local/mysql/bin/mysqladmin ping -uroot -proot -S /tmp/mysql.sock &>/dev/null
                then
                /usr/local/mysql/bin/mysqladmin flush-logs -uroot -proot -S /tmp/mysql.sock
                fi
        endscript
}

 

Logrotate簡介node

logrotate 是Linux系統日誌文件管理工具。用來把舊的日誌文件刪除,並建立新的日誌文件,咱們把它叫作「轉儲」。能夠根據日誌文件的大小,也能夠根據其天數來轉儲,這個過程通常經過 cron 程序來執行。mysql

logrotate 程序還能夠用於壓縮日誌文件,以及發送日誌到指定的E-mail。nginx

默認的logrotate被加入cron的/etc/cron.daily中做爲每日任務執行。sql

/etc/logrotate.d/* 爲/etc/logrotate.conf默認包含目錄其中文件也會被logrotate讀取。指明每一個日誌文件的特定規則。vim

 

/var/lib/logrotate/status中默認記錄logrotate上次輪換日誌文件的時間。bash

 

logrotate 基於Cron運行,因此生成日誌的時間能夠在/etc/crontab中看到;參考:http://huoding.com/2013/04/21/246dom

 

vi /etc/crontab工具

SHELL=/bin/shpost

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binurl

 

# m h dom mon dow user  command

17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly

25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )

52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

 

 

 

logrotate 能夠直接執行,後面跟配置文件就能夠了,如:/usr/sbin/logrotate -f /etc/logrotate.d/nginx

參數:

-v 顯示指令執行過程

-d Debug模式(模擬執行),詳細顯示指令執行過程,便於排錯或瞭解程序執行的狀況。

-f 強制執行

-s <狀態文件>  使用指定的狀態文件

 

logrotate 的默認配置文件是 /etc/logrotate.conf。主要參數:

daily指定轉儲週期爲天天 

weekly指定轉儲週期爲每週 

monthly指定轉儲週期爲每個月 

dateext在文件末尾添加當前日期 

compress經過gzip 壓縮轉儲之後的日誌 

nocompress不須要壓縮時,用這個參數 

copytruncate先把日誌內容複製到舊日誌文件後才清除日誌文件內容,能夠保證日誌記錄的連續性

nocopytruncate備份日誌文件可是不截斷 

create mode owner group轉儲文件,使用指定的文件模式建立新的日誌文件 

nocreate不創建新的日誌文件 

delaycompress和 compress 一塊兒使用時,轉儲的日誌文件到下一次轉儲時才壓縮 

nodelaycompress覆蓋 delaycompress 選項,轉儲同時壓縮。 

errors address專儲時的錯誤信息發送到指定的Email 地址 

ifempty即便是空文件也轉儲,這個是 logrotate 的缺省選項。 

notifempty若是是空文件的話,不轉儲 

mail address把轉儲的日誌文件發送到指定的E-mail 地址 

nomail轉儲時不發送日誌文件 

olddir directory轉儲後的日誌文件放入指定的目錄,必須和當前日誌文件在同一個文件系統 

noolddir轉儲後的日誌文件和當前日誌文件放在同一個目錄下 

rotate count指定日誌文件刪除以前轉儲的次數,0 指沒有備份,5 指保留5 個備份 

tabootext [+] list讓logrotate 不轉儲指定擴展名的文件,缺省的擴展名是:.rpm-orig, .rpmsave, v, 和 ~ 

size size當日志文件到達指定的大小時才轉儲,Size 能夠指定 bytes (缺省)以及KB (sizek)或者MB (sizem). 

prerotate/endscript在轉儲之前須要執行的命令能夠放入這個對,這兩個關鍵字必須單獨成行

postrotate/endscript在轉儲之後須要執行的命令能夠放入這個對,這兩個關鍵字必須單獨成行

 

實例:

crontab -e

59 23 * * *  /usr/sbin/logrotate -f /etc/logrotate.d/nginx      # 把Nginx日誌所有轉儲

 

vi /etc/logrotate.d/nginx

/var/log/nginx/*.log {

        daily#指定轉儲週期爲天天 

        missingok

        rotate 52

        compress#經過gzip 壓縮轉儲之後的日誌

        delaycompress#和 compress 一塊兒使用時,轉儲的日誌文件到下一次轉儲時才壓縮 

        ifempty

        create 0640 www-data adm

        sharedscripts

        postrotate

  #kill -USR1 `cat /var/run/nginx.pid不是停止Nginx的進程,而是傳遞給它信號從新生成日誌,若是nginx沒啓動不作操做

                [ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`

        endscript

}

相關文章
相關標籤/搜索