[精讚] [會員登入]
5261

[CENTOS7] 使用 logrotate 來整理wildfly日誌檔

解決 wildfly 的日誌檔不斷長大的問題

分享此文連結 //n.sfs.tw/12926

分享連結 [CENTOS7] 使用 logrotate 來整理wildfly日誌檔@新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2019-10-25 13:50:25 最後編修
2018-11-22 18:09:54 By 張○○
 

自動目錄

Unix 系統中的日誌檔存放在 /var/log/  中,可供我們查錯及觀察。日誌檔最大的問題是會不斷的長大。

但是其他的日誌檔如果不定時去清空,也許就會長大到非常的可怕的地步,像我曾看過的網頁記錄,才三個月就達3G。

手動清空

如果要將記錄內容清空,移除再建立是一個方法,但不是很好的方法,可以採用下面這個指令

例如:

# cat /dev/null > /var/log/httpd-error.log

或是

# echo '' > /var/log/httpd-error.log

就可以輕鬆的將記錄檔清空。

 

因為架設的 wildfly伺服器的log不斷長大,已經塞爆我的空間,於是採用 logrotate來處理我的記錄檔。

使用 logrotate來定時壓縮及分檔備份

 

一、安裝

系統 centos7

# yum install logrotate

二、設定

logrotate的設定檔為 /etc/logrotate.conf,這裡設定的是全域參數,可以保留不動

# 執行週期
weekly

# 保存幾份資料,4代表會有4+1個
rotate 4

# 壓縮完是否會建一個新的空檔?
create

# 備份檔附檔名加註日期
dateext

# 是否用gz壓縮,預設無
#compress

# 下面是預設的不必動

include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
        minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

各別的設定檔放在 /etc/logrotate.d/

因為wildfly的log檔在 /var/log/wildfly/console.log

建立一個新的設定檔

# cd /etc/logrotate.d/
# cp yum wildfly
# vi wildfly

修改內容如下

/var/log/wildfly/console.log {
    missingok
    notifempty
    copytruncate
    rotate 10
    compress
    size 300M
    daily
    create 0600 root root
}

missingok 如果找不到 log 檔也沒關係
notifempty 如果檔案為0就略過
copytruncate 先複製 log 檔的內容再清空,因為wildfly會寫到原檔,這個參數一定要設定。
rotate 10  建立10個備份,注意這不算入原本那個
compress  壓縮成gz
size 300M 至少要300m才建立備份

三、測試

# logrotate -f /etc/logrotate.conf

<正確的話沒任何的回應>

# logrotate -f /etc/logrotate.d/wildfly

 

四、加入排程

不必加入排程,因為yum 裝完後就已經有在 /etc/cron.daily/ 中存在

除非你要改成自己的排程

10 0 * * 0 /usr/sbin/logrotate /etc/logrotate.conf

 

錯誤排除

error: error creating output file /var/log/wildfly/console.log.X: File exists

建立時已經有檔案,把該檔移除即可

 

參考連結

[1] [FREEBSD] 使用 logrotate 來管理及備份日誌檔@新精讚

[2] https://ihower.tw/blog/archives/3565

[3] https://www.jianshu.com/p/ea7c2363639c 這裡有參數的說明

 

 

END

你可能感興趣的文章

[Centos7] audit 服務 在紅帽和suse的linux中有一個audit這樣的daemon,寫入的是核心層的記錄。

[Centos7] 使用網路安裝 Centos7 使用網路來安裝Centos7,雖然這不是什麼新鮮事,但可以省你的磁碟空間或光碟片

[Centos7] 安裝sphinx+php7 在centos7上安裝sphinx,在PHP7上運作正確

[Centos8] 網頁化的系統管理介面 cockpit Centos8 提供一個網頁化的管理介面,稱為駕駛艙,值得一試

移除ssl 私鑰的密碼 現在用ssl產生私鑰會需要設定密碼,但是私鑰有密碼,啟動apache會失敗。

[Linux] 怎麼修改終端機console下ls的顏色? 在linux下的ls使用--color的參數時,雖然目錄有上色,但是顏色是深藍色很難辨識,要怎麼修改呢?

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

談借錢 人借錢時手心向上頭向下,人還錢時手心向下頭向上

看懂DSUB DVI HDMI USB等各式影音接頭 看懂DSUB DVI HDMI等各式影音接頭

TFTP Server 安裝及使用 讓設備的網路設定檔或是韌體經由TFTP拷備出來,操作的方法

[Mysql] 資料型態int, float, double, text, char, varchar, blob大小 Mysql中資料型態int, float, text, char, varchar, blob大小

Sound of Music @比利時中央車站 比利時中央車站的快閃表演:「真善美」