Logrotate工具使用

2022-03-17 11:27:37 字數 3039 閱讀 1290

​  logrotate是乙個被設計來簡化系統管理日誌檔案的工具,在系統執行時,如果產生大量的日誌檔案,可以使用該工具進行管理,如/var/log/*資料夾是儲存系統和應用日誌的目錄,如果某些日誌檔案沒有設定歸檔,可能會一直儲存變大導致伺服器磁碟空間不足。logrotate是開源的自由軟體,可以在github上檢視到詳細介紹。logrotate

1. 安裝

​  我在centos6和ubuntu18.04上看到都是預設安裝好的了,如果沒有安裝,可以利用各個系統的安裝源進行安裝:

yum install logrotate #centos使用安裝源安裝

apt install logrotate #ubuntu使用安裝源安裝

###當然也可以使用github上的原始碼編譯安裝

2. 使用

如果安裝好logrotate,會在/etc/目錄下有logrotate.conf檔案和logrotate.d目錄,logrotate.conf是預設的配置檔案,logrotate.d目錄儲存了一些自定義的配置檔案。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 2 weeks worth of backlogs 保留兩個歸檔檔案,因為每週執行一次。即只有兩周的歸檔檔案進行保留

rotate 2

# create new (empty) log files after rotating old ones 歸檔後建立新的檔案

create

# uncomment this if you want your log files compressed 是否壓縮歸檔的日誌檔案,這裡注釋了compress,預設就是不壓縮

#compress

# packages drop log rotation information into this directory

include /etc/logrotate.d #包含/etc/logrotate.d目錄下的配置規則

# no packages own wtmp, or btmp -- we'll rotate them here

/var/log/wtmp

/var/log/btmp

# system-specific logs may be configured here

上面貼出了logrotate.conf的配置檔案資訊,我們也可以看到logrotate.d目錄下定義的配置,如下面的syslog(/etc/logrotate.d/syslog)檔案:

/var/log/cron         #匹配/var/log/cron,/var/log/maillog,

/var/log/maillog #/var/log/messages等檔案,也可以使用*進

/var/log/messages #行模糊匹配

/var/log/secure

/var/log/spooler

​  有了配置檔案,那麼就可以讓logrotate使用配置檔案執行了。

logrotate -v /etc/logrotate.conf      #根據配置檔案執行,如果已經執行過了,預設不會再次執行,-v為--verbose

logrotate -vf /etc/logrotate.d/syslog #強制根據某一配置檔案執行-f為--force

#引數可以使用--help檢視

logrotate --help

usage: logrotate [option...] -d, --debug don't do anything, just test (implies -v)

-f, --force force file rotation

-m, --mail=command command to send mail (instead of `/bin/mail')

-s, --state=statefile path of state file

-v, --verbose display messages during rotation

help options:

-?, --help show this help message

--usage display brief usage message

#或者檢視手冊,裡面也有配置的sample和各個引數介紹

man logrotate

3. 引數介紹

引數說明

daily

表示每天執行歸檔一次,除此還有『weekly』,『monthly』,『yearly』

rotate 2

表示保留2個歸檔檔案

dateext

表示歸檔的檔案以日期命名,如yyyymmdd

compress

歸檔的檔案啟用壓縮,預設為gzip壓縮

nocompress

不啟用壓縮

create mode owner group, create owner group

歸檔日誌檔案後建立新檔案的訪問許可權、所屬使用者和組等,如create 600 root root

missingok

如果歸檔的日誌檔案不存在,不提示錯誤資訊

prerotate...endscript

在歸檔前執行prerotate和endscript裡面指定的批處理

postrotate...endscript

在歸檔後執行postrotate和endscript裡面指定的批處理

logrotate工具清理日誌

伺服器使用例項 vim etc logrotate.d tomcat 寫入一下內容 home tomcat logs catalina.out 儲存,然後執行 usr sbin logrotate f etc logrotate.conf 該命令執行後,應該是作為程序一直在執行,重啟伺服器可能需要再...

日誌分割工具logrotate

eoflogrotate d etc logrotate.d mymon 測試 logrotate vf etc logrotate.d mymon 強制執行簡單說明引數daily 每日執行,weekly,yearly rotate 5 儲存幾份 missingok 忽略logrotate執行時的錯...

使用Logrotate管理日誌

linux使用某些軟體的時候會產生日誌檔案,而這些軟體本身對日誌不進行分割或者壓縮處理,久而久之會導致日誌檔案異常巨大,影響機器效能,配置不高的機器上尤為嚴重。而logrotate就是管理這些日誌檔案的神器。logrotate會週期性的讀,壓縮,備份,建立新的日誌檔案。你想對日誌檔案做的操作,它基本...