在Linux系统上跑任务会遇到系统磁盘空间爆满的情况,表现出来的现象是程序运行报错,或执行缓慢。记录下Linux磁盘空间占用的查看方法和文件清理脚本定时触发配置的实现。
一、Linux磁盘空间占用分析
这边用df和du命令配合来查找占用磁盘空间的大头,找到文件目录之后,再根据文件类型决定是删除还是对磁盘扩容。
首先使用df -h命令查看磁盘整体的占用百分比和占用大小情况,先确定是哪个文件夹占用较多,如果文件较多命令执行可能很慢,这边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操作。
转载请注明出处:陈文管的博客 – Linux磁盘空间查看及定时清理配置
扫码或搜索:文呓
微信公众号 扫一扫关注
发表评论