Linux磁盤空間爆滿怎么辦?定時文件清理腳本配置實現


在Linux系統上跑任務會遇到系統磁盤空間爆滿的情況,表現出來的現象是程序運行報錯,或執行緩慢。記錄下Linux磁盤空間占用的查看方法和文件清理腳本定時觸發配置的實現。

一、Linux磁盤空間占用分析

這邊用df和du命令配合來查找占用磁盤空間的大頭,找到文件目錄之后,再根據文件類型決定是刪除還是對磁盤擴容。

首先使用df -h命令查看磁盤整體的占用百分比和占用大小情況, 先確定是哪個文件夾占用較多,

~$ df -h

文件系統          容量  已用  可用  已用% 掛載點

udev            16G    0  16G    0% /dev

tmpfs          3.2G  11M  3.2G    1% /run

/dev/nvme0n1p2  234G  182G  40G  83% /

tmpfs            16G    0  16G    0% /dev/shm

tmpfs          5.0M  4.0K  5.0M    1% /run/lock

tmpfs            16G    0  16G    0% /sys/fs/cgroup

可以看到當前根目錄占用83%,接着使用du命令繼續查找占用大頭的文件夾,du的–max-depth=1表示只展示第一個層級的目錄和文件,sort的-h選項和du的-h選項一樣,-r表示倒敘,默認升序。

~$ du -h / --max-depth=1 | sort -hr | head -n 10

186G /

161G /home

6.3G /usr

5.9G /var

4.3G /lib

4.0G /tmp

3.8G /snap

418M /opt

375M /boot

288M /bin

可以看出 /home目錄占用大頭, 接着繼續查看/home目錄下的文件占用:

~$ du -h /home --max-depth=1 | sort -hr | head -n 10

161G /home

150G /home/arc

4.0K /home/Systemback

~$ du -h /home/arc --max-depth=1 | sort -hr | head -n 10

150G /home/arc

90G /home/arc/lizr

27G /home/arc/WebstormProjects

8.7G /home/arc/arc-log

7.5G /home/arc/arc-resources

5.1G /home/arc/autoPressureLog

4.0G /home/arc/atf_runner

1.3G /home/arc/AtfCaseServer

524M /home/arc/.local

522M /home/arc/ArcCaseServer

~$ du -h /home/arc/lizr --max-depth=1 | sort -hr | head -n 10

90G /home/arc/lizr/master

90G /home/arc/lizr

4.0K /home/arc/lizr/protocol

~$ du -h /home/arc/lizr/master --max-depth=1 | sort -hr | head -n 10

90G /home/arc/lizr/master

45G /home/arc/lizr/master/atf_runner

44G /home/arc/lizr/master/atf_report

1.1G /home/arc/lizr/master/atf

108K /home/arc/lizr/master/atf_pre

20K /home/arc/lizr/master/atf_apk

現在可以知道是 atf_runner和atf_report占用最多,接着可以用ls -l查看這兩個文件夾下面有哪些文件

~$ ls -l /home/arc/lizr/master/atf_report/

總用量 24772

drwxrwxr-x 4 arc arc 4096 3月  7 01:45 20210307_014457514410

drwxrwxr-x 4 arc arc 4096 3月  7 01:47 20210307_014601203246

drwxrwxr-x 4 arc arc 4096 3月  7 01:48 20210307_014736618057

drwxrwxr-x 4 arc arc 4096 3月  7 01:50 20210307_014900881721

....

~$ ls -l /home/arc/lizr/master/atf_runner/

比如這邊查看在atf_report目錄下有6193個報告文件,都是以日期命名的文件夾,有如下兩種方式批量刪除文件

1)用日期前綴刪除較早的文件,例如把 20210307開頭的文件夾都刪除:

rm -rf /home/arc/lizr/master/atf_report/20210307*

2)保留最近5天的文件,5天之前的文件都清除,例如保留atf_report目錄下最近5天的文件,超過5天的都刪除,要保留不同的天數,配置不同的-mtime參數就行,也可以同時對刪除的文件名做一個匹配過濾,這邊配置‘*’不做過濾:

find /home/arc/lizr/master/atf_report/ -mtime +5 -name '*' -exec rm -rf {} \;

二、定時文件清理腳本配置實現

1、Jenkins配置定時觸發

比較簡便的方式是配置Jenkins,在項目里面加上清理文件的腳本,Jenkins執行流水線上加上腳本的執行,定時任務配置見: Jenkins時區配置及定時構建 。

2、Linux cron配置周期性觸發

cron進程是Linux中的一個守護進程,一般用來執行系統中的周期性任務, 首先查看cron進程,/usr/sbin/ cron 就是當前運行的cron進程。

~$ ps -ef | grep cron

arc       2533299 2533276   0 00:23 pts/1     00:00:00 grep –color=auto cron

root     2591934       1   0 2月07 ?       00:00:05 /usr/sbin/ cron -f

如果cron進程不存在,用以下命令開啟或停止。

# 開啟服務

sudo service cron start

# 停止服務

sudo service cron stop

接着編輯crontab文件,crontab是UNIX系統下定期執行任務的觸發器,要定期執行的任務都可以在這個文件里面配置,用以下命令打開文件,第一次使用這個命令會讓你選擇編輯器,選擇vim比較方便

sudo crontab -e

加入要執行的腳本參數配置:

# Edit this file to introduce tasks to be run by cron.

#

# Each task to run has to be defined through a single line

# indicating with different fields when the task will be run

# and what command to run for the task

#

# To define the time you can provide concrete values for

# minute (m), hour (h), day of month (dom), month (mon),

# and day of week (dow) or use '*' in these fields (for 'any').

#

# Notice that tasks will be started based on the cron's system

# daemon's notion of time and timezones.

#

# Output of the crontab jobs (including errors) is sent through

# email to the user the crontab file belongs to (unless redirected).

#

# For example, you can run a backup of all your user accounts

# at 5 a.m every week with:

# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

#

# For more information see the manual pages of crontab(5) and cron(8)

#

# m h  dom mon dow  command

0 0 * * * /home/script/timer_cache_clean.sh       

這邊定時配置的語法和Jenkins定時觸發構建是一樣的,只是在5個時間參數后面加上要執行的命令。

* * * * * <command>

之后保存修改,退出文件編輯,重啟cron就配置完成了。

sudo service cron restart

關於sh腳本里面清理文件的配置可以參考上面find -exec操作。

文章轉載:陳文管的博客– https://www.chenwenguan.com/linux-space-analysis-and-timer-clean/

最后,特別推薦一個分享C/C++和算法的優質內容,學習交流,技術探討,面試指導,簡歷修改...還有超多源碼素材等學習資料,零基礎的視頻等着你!

還沒關注的小伙伴,可以長按關注一下:


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM