CentOS Linux使用logrotate分割管理日誌

來源:互聯網
上載者:User

標籤:centeos   logrotate   

logrotate程式是一個記錄檔管理工具。用於分割記錄檔,刪除舊的記錄檔,並建立新的記錄檔,起到“轉儲”作用。可以節省磁碟空間。

logrotate命令格式:
logrotate [OPTION...] <configfile>
-d, --debug :debug模式,測試組態檔案是否有錯誤。
-f, --force :強制轉儲檔案。
-m, --mail=command :發送日誌到指定郵箱。
-s, --state=statefile :使用指定的狀態檔案。
-v, --verbose :顯示轉儲過程。

logrotate的設定檔是/etc/logrotate.conf。查看預設配置情況:

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

# 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 -- we‘ll rotate them here
/var/log/wtmp {
monthly
minsize 1M
create 0664 root utmp
rotate 1
}

# system-specific logs may be also be configured here.

簡單說明:
weekly :所有的記錄檔每周轉儲一次。
rotate 4 :轉儲的檔案分為4份。
create :logrotate自動建立新的記錄檔。
compress :壓縮記錄檔。預設是注釋掉的。
include /etc/logrotate.d :讀入/etc/logrotate.d目錄下的日誌轉儲參數,當系統中安裝了RPM軟體包時,RPM包的日誌轉儲參數一般會自動建立在/etc/logrotate.d目錄下。
/var/log/wtmp段 :對/var/log/wtmp日誌轉儲的配置。

使用logrotate管理lnmp一鍵安裝包中nginx的串連日誌,lnmp記錄檔在/home/wwwlogs目錄下。

建立設定檔:

vim /etc/logrotate.d/nginx

輸入如下:

/home/wwwlogs/access.log /home/wwwlogs/nginx_error.log {
notifempty
daily
rotate 5
sharedscripts
postrotate
/bin/kill -HUP `/bin/cat /usr/local/nginx/logs/nginx.pid`
endscript
}

說明:
notifempty :如果是空檔案的話,不轉儲。
daily :記錄檔每天轉儲一次。
rotate 5 ;轉儲檔案分為5份。
postrotate/endscript :日誌轉儲後執行的指令碼。這裡用來讓nginx重建記錄檔。nginx.pid裡存的是nginx的主進程號。

執行logrotate:

/usr/sbin/logrotate -vf /etc/logrotate.conf

如果沒有報錯,產生了轉儲檔案,nginx正常訪問,就OK了。

logrotate如何自動執行:
在/etc/cron.daily目錄下有logrotate執行的指令碼。通過crontab程式每天執行一次。


本文出自 “記錄工作學習的軌跡” 部落格,請務必保留此出處http://stephenliu.blog.51cto.com/6470474/1564553

CentOS Linux使用logrotate分割管理日誌

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.