標籤: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分割管理日誌