欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何用logrotate管理每日增長的日志

 更新時間:2023年05月11日 09:37:32   作者:百度眾測  
這篇文章主要介紹了如何用logrotate管理每日增長的日志問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

logrotate簡介

logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.

這是logrotate文檔中對其自身的描述,其實logrotate功能很簡單,正如其名,按一定周期自動循環(huán)分割和保存日志防止無限增長,必要的時候進行壓縮、刪除,還可以進行郵件通知等功能。

很多服務器上總會運行著一些會不停生成成噸日志的程序,最簡單的比如nginx、httpd之類的web服務器的訪問日志,為了保持這些日志的可檢索以及防止無限增長把有限的硬盤都吃干凈了,就會有五花八門的cron腳本來處理這些日志,事實上這些事情都可以由logrotate代勞。

安裝logrotate

事實上大多數(shù)Linux發(fā)行版本都默認安裝了logrotate,可以通過以下命令來判斷系統(tǒng)是否已經(jīng)預裝了logrotate:

rpm包管理器:

rpm -qa|grep logrotate

dpkg包管理器:

dpkg --list|grep logrotate

更簡單粗暴的方法:

which logrotate

如果能看到有字符串返回的話,你的服務器就應該預裝了logrotate,并且它已經(jīng)每天在默默地工作了,只不過你并沒有給他分配活兒。

如果沒有安裝的話,用正常的包管理工具都能安裝它:

yum install logrotateapt-get install logrotate

logrotate基本工作原理

作為一個定時處理的管理工具,logrotate本身是個命令行的工具,然而它需要定時處理你的日志,所以顯然logrotate還是得基于cron來工作的,否則就沒有人定時來喚醒它讓它干活了,正常情況下logrotate已經(jīng)作為cron.daily的一個任務每天被定時在執(zhí)行了,這個定時腳本位于/etc/cron.daily/logrotate

#!/bin/sh
# Clean non existent log file entries from status file
cd /var/lib/logrotate
test -e status || touch status
head -1 status > status.clean
sed 's/"http://g' status | while read logfile date
do
    [ -e "$logfile" ] && echo "\"$logfile\" $date"
done >> status.clean
mv status.clean status
test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf

上面這段是Ubuntu中默認的logrotate腳本,各個不同版本上的logrotate腳本會有細微的不同,但是核心都是使用/etc/logrotate.conf配置腳本來啟動logrotate。

然而這段腳本只是位于這個目錄里,為什么會被每天調(diào)用呢,而且每天什么點調(diào)用呢?

可以查看CRON的默認配置文件/etc/crontab(新版CentOS中其默認配置文件位于/etc/anacrontab)的內(nèi)容:

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# 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 )

事實上CRON默認會存在這4條指令,分別是每小時、每天、每周、每個月會執(zhí)行對應目錄下的腳本,而不需要再通過日常常用的crontab去配置,可以看到cron.daily目錄下的腳本在每天的6點25分會被執(zhí)行,這也就意味著在這臺機器上每天6點25分會啟動logrotate進行日志的處理,各個系統(tǒng)的默認配置也是各不相同的。

最后來看一下默認的配置文件/etc/logrotate.conf:

# see "man logrotate" for details
# rotate log files weekly
weekly
# use the syslog group by default, since this is the owning group
# of /var/log/syslog.
su root syslog
# 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
# packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own wtmp, or btmp -- we'll rotate them here
/var/log/wtmp {
    missingok
    monthly
    create 0664 root utmp
    rotate 1
}
/var/log/btmp {
    missingok
    monthly
    create 0660 root utmp
    rotate 1
}
# system-specific logs may be configured here

其實默認配置文件中給出的是logrotate的一些默認配置選項,并給出了一些樣例的配置(如wtmp,btmp),這些配置的意義在后文中會予以闡述,而這個配置文件中還包含了include /etc/logrotate.d,這使得我們可以不用將所有的配置扎堆寫入這個默認配置文件中,而是可以分散地管理在/etc/logrotate.d目錄下,事實上/etc/logrotate.d也已經(jīng)包含了一些樣例的配置文件供參考。

logrotate的配置和使用

如同剛才所說的,logrotate會每日CRON啟動并執(zhí)行,我們只需要在/etc/logrotate.d目錄中添加我們需要的配置,就可以利用logrotate來自動管理我們需要的日志文件。

這里先來看一個目錄下已經(jīng)存在的樣例配置dpkg:

/var/log/dpkg.log {
    monthly
    rotate 12
    compress
    delaycompress
    missingok
    notifempty
    create 644 root root
}
/var/log/alternatives.log {
    monthly
    rotate 12
    compress
    delaycompress
    missingok
    notifempty
    create 644 root root
}

正如其名稱dpkg,這個配置是用來處理包管理器dpkg的默認日志的,處理的日志文件是/var/log/dpkg.log與/var/log/alternatives.log這兩個文件

其配置內(nèi)容如下:

  • monthly:按月處理日志
  • rotate 12:保留12份日志
  • compress:日志分割后會進行gzip壓縮
  • delaycompress:日志壓縮會被延后到下次分割時進行
  • missingok:目標日志文件不存在程序也不會報錯退出
  • notifempty:目標日志文件為空時不進行分割操作
  • create 644 root root:以644也就是rw-r–r–權(quán)限來建立分割出來的文件,同時該分割文件所屬用戶為root,用戶組為root

在/var/log下列出文件列表,可以看到dpkg的日志的確被分割并壓縮了:

這里寫圖片描述

仿照這個樣例文件,我們可以配置一個類似的處理任務,這里處理一下nginx的log文件,一般web serve的access log文件都是日志重災區(qū),建立一個/etc/logrotate.d/nginx配置文件,內(nèi)容如下:

/home/wwwlogs/*.log {
    daily
    rotate 7
    dateext
    compress
    delaycompress    
    missingok
    notifempty
    create 644 root root
    sharedscripts
    postrotate
        kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
    endscript
}

與之前的樣例配置類似,注意到聲明日志文件位置時使用了*通配符,這意味著/home/wwwlogs/下的所有.log文件都會被logrotate處理,這里多出了幾行配置

說明如下:

  • daily:每天處理日志
  • dateext:備份的日志文件的后綴不再是默認的.1 .2 .3的遞增格式,而是.YYYYMMDD的日期格式
  • sharedscripts:配置該選項時,prerotate和postrotate段的腳本會在所有文件被處理結(jié)束(被處理前)統(tǒng)一執(zhí)行,而不是每個文件前后分別執(zhí)行一次
  • postrotate:處理日志后鉤子,postrotate和endscript構(gòu)成一個shell語句塊,在日志文件被處理后這部分語句會被執(zhí)行,對應的還有prerotate語句塊

這樣nginx的log文件的處理配置就完成了,但是我們可能還需要確認一下配置的內(nèi)容到底是否正確呢,這時候可以利用logrotate命令行的debug選項來進行測試

命令如下:

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

開啟了debug選項時,logrotate會詳細地給出處理日志過程中的處理信息,但是并不會真正地去處理日志文件,所以可以用來測試配置文件處理的是否正確

這行命令的輸出如下:

reading config file /etc/logrotate.d/nginx
Handling 1 logs
rotating pattern: /home/wwwlogs/*.log  forced from command line (7 rotations)
empty log files are not rotated, old logs are removed
considering log /home/wwwlogs/access.log
  log needs rotating
considering log /home/wwwlogs/jp01.sanaecon.com.log
  log needs rotating
considering log /home/wwwlogs/nginx_error.log
  log needs rotating
rotating log /home/wwwlogs/access.log, log->rotateCount is 7
dateext suffix '-20151108'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding logs to compress failed
glob finding old rotated logs failed
rotating log /home/wwwlogs/jp01.sanaecon.com.log, log->rotateCount is 7
dateext suffix '-20151108'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding logs to compress failed
glob finding old rotated logs failed
rotating log /home/wwwlogs/nginx_error.log, log->rotateCount is 7
dateext suffix '-20151108'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding logs to compress failed
glob finding old rotated logs failed
renaming /home/wwwlogs/access.log to /home/wwwlogs/access.log-20151108
creating new /home/wwwlogs/access.log mode = 0644 uid = 0 gid = 0
renaming /home/wwwlogs/jp01.sanaecon.com.log to /home/wwwlogs/jp01.sanaecon.com.log-20151108
creating new /home/wwwlogs/jp01.sanaecon.com.log mode = 0644 uid = 0 gid = 0
renaming /home/wwwlogs/nginx_error.log to /home/wwwlogs/nginx_error.log-20151108
creating new /home/wwwlogs/nginx_error.log mode = 0644 uid = 0 gid = 0
running postrotate script
running script with arg /home/wwwlogs/*.log : "
        kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
"

可以看到logrotate如我們設想的那樣在正常處理日志文件,至此一個logrotate配置就完成了。

logrotate命令行除了可以用來展示配置文件配置是否正確以外,還可以用來手動執(zhí)行日志分割,使用以下命令行:

logrotate -f /etc/logrotate.d/nginx

就會脫離CRON手動運行一次日志分割和處理任務,事實上通過這個方式也可以用第三方的其他程序來管理日志分割的頻率。

本文中給出了一些常見的配置文件中的配置參數(shù)的含義,更多其他的配置參數(shù)的含義和功能可以參考官方的配置文檔:http://linuxconfig.org/logrotate-8-manual-page

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Linux usermod 命令的使用

    Linux usermod 命令的使用

    這篇文章主要介紹了Linux usermod 命令的使用,幫助大家更好的理解和學習Linux,感興趣的朋友可以了解下
    2020-08-08
  • LINUX入門級常用命令20條整理

    LINUX入門級常用命令20條整理

    下面總結(jié)LINUX初學者常用的20條命令,不過沒有加例子,等有時間進行補充:
    2008-08-08
  • Apache本地二級域名配置方法

    Apache本地二級域名配置方法

    這篇文章主要介紹了Apache本地二級域名配置方法,涉及hosts文件設置與Apache配置文件設置等相關技巧,需要的朋友可以參考下
    2016-05-05
  • Centos7硬盤掛載方法

    Centos7硬盤掛載方法

    這篇文章主要介紹了Centos7硬盤掛載方法并把需要注意的地方做了注明,需要的朋友參考下吧。
    2018-02-02
  • 使用?Apache?Dubbo?實現(xiàn)遠程通信(微服務架構(gòu))

    使用?Apache?Dubbo?實現(xiàn)遠程通信(微服務架構(gòu))

    Apache?Dubbo?是一個分布式服務框架,主要實現(xiàn)多個系統(tǒng)之間的高性能、透明化調(diào)用,本文給大家介紹使用?Apache?Dubbo?實現(xiàn)遠程通信的詳細過程,感興趣的朋友跟隨小編一起看看吧
    2022-01-01
  • Apache加速模塊mod_pagespeed安裝使用詳細介紹

    Apache加速模塊mod_pagespeed安裝使用詳細介紹

    這篇文章主要介紹了Apache加速模塊mod_pagespeed安裝使用詳細介紹,需要的朋友可以參考下
    2014-03-03
  • Linux ln 命令的使用

    Linux ln 命令的使用

    這篇文章主要介紹了Linux ln 命令的使用,幫助大家更好的理解和使用Linux,感興趣的朋友可以了解下
    2020-08-08
  • Linux上進行常用軟件的配置方法

    Linux上進行常用軟件的配置方法

    這篇文章主要介紹了Linux上進行常用軟件的配置方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • Linux中的EXT系列文件系統(tǒng)格式詳解

    Linux中的EXT系列文件系統(tǒng)格式詳解

    這篇文章主要給大家介紹了關于Linux中EXT系列文件系統(tǒng)格式的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Linux系統(tǒng)具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-06-06
  • Centos7.3下vsftp服務的安裝方法

    Centos7.3下vsftp服務的安裝方法

    這篇文章主要為大家詳細介紹了Centos7.3下vsftp服務的安裝方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09

最新評論