如何在Centos 7上用Logrotate管理日誌文件

何爲Logrotate?php

Logrotate是一個實用的日誌管理工具,旨在簡化對系統上生成大量的日誌文件進行管理。 Logrotate容許自動旋轉壓縮,刪除和郵寄日誌文件,從而節省寶貴的磁盤空間。 Logrotate能夠設置爲天天、每週、每個月或當日志文件達到必定的大小時處理日誌文件。還能夠徹底控制日誌的自動化管理方式,而不須要人工干預。Logrotate支持Linux系統上的全部日誌文件,包括但不限於ApacheNginxTomcatELKzabbix等應用。node

1.安裝logrotatepython

系統默認已經安裝,某些Linux發行版可能並未安裝,那麼請執行如下命令安裝:nginx

Centos:apache

$ yum install logrotate -y

Ubuntu:vim

$ apt-get install logrotate -y

經過yum默認安裝的文件存儲位置說明tomcat

/etc/cron.daily            # logrotate腳本文件存放位置
/etc/logrotate.conf        # 主配置文件
/usr/sbin/logrotate        # 二進制程序文件
/etc/logrotate.d/          # 特定服務日誌存儲目錄(本身設定的配置文件)
/var/lib/logrotate.status  # 狀態文件

2.查看logrotate主文件默認配置狀況bash

$ cat /etc/logrotate.conf

# 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                     # rotate後,建立一個新的空文件

# 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 {             # 指定/var/log/wtmp日誌文件;
    monthly                 # 每個月輪轉一次,優先於全局設定的每週輪轉一次;
    minsize 1M              # 日誌文件大於1M纔會去輪轉;
    create 0664 root utmp   # 新日誌文件的權限,屬主,屬組;
    rotate 1                # 保留一個日誌備份,優先於全局設置的四個;
}
/var/log/btmp {             # 指定/var/log/btmp日誌文件;
    missingok               # 若是日誌丟失,不報錯;
    monthly
    create 0600 root utmp
    rotate 1
}

# system-specific logs may be also be configured here.
# 系統特定的日誌也能夠在主文件中配置。

3.logrotate日誌切割dom

在定義日誌文件時可使用通配符,但不建議使用,由於它會包括已切換過的日誌,如必須使用,請在*號後加上已知的文件擴展名, 例如:*.logide

Nginx日誌:

/etc/logrotate.d/目錄下建立Nginx服務日誌配置文件

$ vim /etc/logrotate.d/nginx /usr/local/nginx/logs/*.log { create 644 www root daily dateext rotate 3 minsize 10K copytruncate nocompress missingok notifempty noolddir postrotate /bin/kill -USR1 `cat /usr/local/nginx/logs/nginx.pid` endscript }

php日誌:

/usr/local/php/var/log/*.log { missingok notifempty sharedscripts delaycompress create 0664 www www postrotate /bin/kill -SIGUSR1 `cat /usr/local/php/var/run/php-fpm.pid 2>/dev/null` 2>/dev/null || true endscript }

注:你也能夠手動生成一個20M內容的日誌文件進行測試,例如:

$ head -c 20M < /dev/urandom > /var/log/renwole-log

配置完成後,能夠經過以下命令來手動執行查看效果:

$ logrotate -f /etc/logrotate.d/nginx

另外還可使用以下命令查看logrotate運行狀態:

$ cat /var/lib/logrotate/logrotate.status

因爲Logrotate是基於cron定時運行的,因此logrotate腳本默認在 /etc/cron.daily/ 目錄下,文件名是logrotate。你能夠設置 /etc/anacrontab 來控制Logrotate什麼時候運行。

4.查看anacrontab默認配置

$ cat /etc/anacrontab

# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days   delay in minutes   job-identifier   command
1       5       cron.daily              nice run-parts /etc/cron.daily
7       25      cron.weekly             nice run-parts /etc/cron.weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly

5.關於logrotate經常使用配置參數註解

daily,weekly,monthly  # 轉儲週期分別是天天/每週/每個月;
minsize 15M           # 日誌體積大於此值時輪換(例如:100K,4M);
dateext	              # 輪詢的文件名字帶有日期信息;
missingok             # 若是日誌文件丟失,不要顯示錯誤;
rotate 5              # 輪轉存儲中包含多少備份日誌文件,0爲無備份,以數字爲準;
compress              # 經過gzip壓縮轉儲之後的日誌,以*.gz結尾;
nocompress            # 不須要壓縮時,用這個參數;
delaycompress         # 延遲壓縮,和compress一塊兒使用時壓縮全部日誌,除當前和下一個最近的;
nodelaycompress       # 覆蓋delaycompress選項,轉儲同時壓縮;
copytruncate          # 用於還在打開中的日誌文件,把當前日誌備份並截斷;
nocopytruncate        # 備份日誌文件可是不截斷;
create 644 www root   # 轉儲文件,使用指定的文件模式建立新的日誌文件;
nocreate              # 不創建新的日誌文件;
errors renwole@my.org # 專儲時的錯誤信息發送到指定的Email地址;
ifempty               # 即便是空文件也轉儲,這個是logrotate的缺省選項;
notifempty            # 若是日誌文件爲空,則不轉儲;
mail renwole@my.org   # 把轉儲的日誌文件發送到指定的E-mail地;
nomail                # 轉儲時不發送日誌文件;
olddir /tmp           # 轉儲後的日誌文件放入指定目錄,必須和當前日誌文件在同一個文件系統;
noolddir              # 轉儲後的日誌文件和當前日誌文件放在同一個目錄下;
prerotate/endscript   # 在轉儲之前須要執行的命令能夠放入這個對,這兩個關鍵字必須單獨成行;
postrotate/endscript  # 在轉儲之後須要執行的命令能夠放入這個對,這兩個關鍵字必須單獨成行;
tabooext              # 不轉儲指定擴展名的文件,缺省擴展名:cfsaved,.disabled,.dpkg-dist等;
sharedscripts         # 共享腳本,讓postrotate/endscript包含腳本只執行一次便可;
dateformat            # 配合dateext使用能夠爲切割後的日誌加上YYYYMMDD格式的日期;

以上參數均可以在全局主配置文件中定義,或指定爲某個日誌文件進行配置,注意:使用時參數之間不要衝突。

 

python uwsgi 日誌分割

[root@test-server28 logrotate.d]# cat uwsgi
/data/logs/uwsgi/*/access.log {
# 天天分割
daily
# 保存30天
rotate 30
# 日期後綴
dateext
# 日誌丟失不報錯
missingok
# 空文件不轉儲
notifempty
# 壓縮
#compress
# 共享腳本
sharedscripts
postrotate
touch /data/logs/uwsgi/touchforlogrotate
endscript
}

 

切割日誌時出現報錯 

error: skipping "/home/deploy/tomcat/apache-tomcat-7.0.75-df/logs/catalina.out" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.

  

xx 文件所屬用戶

添加「su deploy deploy 」到/etc/logrotate.d/tomcat文件中便可 
以下;


/home/deploy/tomcat*********/logs/*.out {
        su deploy deploy
        copytruncate
        daily
        rotate 30
        dateext
        missingok
        notifempty
}

  

[root@**** logrotate.d]#  logrotate -f /etc/logrotate.d/tomcat


[root@**** logs]# ls
 catalina.out            catalina.out-20181210

  

 

對分割日誌進行定時的刪除 

#!/bin/bash


/bin/find /mnt/log/tomcat/ -name "*.out*" -mtime +15 -type f|xargs rm -rf

/bin/find /home/deploy/*******/logs/  -name "*.out*" -mtime +1 -type f -print0 |xargs -0 mv -t /mnt/log/tomcat/
相關文章
相關標籤/搜索