分享

Linux磁盘空间查看及定时清理配置

 青柠二锅头 2021-12-27

在Linux系统上跑任务会遇到系统磁盘空间爆满的情况,表现出来的现象是程序运行报错,或执行缓慢。记录下Linux磁盘空间占用的查看方法和文件清理脚本定时触发配置的实现。

一、Linux磁盘空间占用分析

这边用df和du命令配合来查找占用磁盘空间的大头,找到文件目录之后,再根据文件类型决定是删除还是对磁盘扩容。

首先使用df -h命令查看磁盘整体的占用百分比和占用大小情况,先确定是哪个文件夹占用较多,如果文件较多命令执行可能很慢,这边df命令如果没加-h参数,输出的容量、已用和可用数据就是没转化的字节大小。


~$ df -h文件系统          容量   已用   可用  已用% 挂载点udev             16G     0   16G    0% /devtmpfs           3.2G   11M  3.2G    1% /run/dev/nvme0n1p2  234G  182G   40G   83% /tmpfs            16G     0   16G    0% /dev/shmtmpfs           5.0M  4.0K  5.0M    1% /run/locktmpfs            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 10186G /161G /home6.3G /usr5.9G /var4.3G /lib4.0G /tmp3.8G /snap418M /opt375M /boot288M /bin

可以看出/home目录占用大头,接着继续查看/home目录下的文件占用:


~$ du -h /home --max-depth=1 | sort -hr | head -n 10161G /home150G /home/arc4.0K /home/Systemback
~$ du -h /home/arc --max-depth=1 | sort -hr | head -n 10150G /home/arc90G /home/arc/lizr27G /home/arc/WebstormProjects8.7G /home/arc/arc-log7.5G /home/arc/arc-resources5.1G /home/arc/autoPressureLog4.0G /home/arc/atf_runner1.3G /home/arc/AtfCaseServer524M /home/arc/.local522M /home/arc/ArcCaseServer
~$ du -h /home/arc/lizr --max-depth=1 | sort -hr | head -n 1090G /home/arc/lizr/master90G /home/arc/lizr4.0K /home/arc/lizr/protocol
~$ du -h /home/arc/lizr/master --max-depth=1 | sort -hr | head -n 1090G /home/arc/lizr/master45G /home/arc/lizr/master/atf_runner44G /home/arc/lizr/master/atf_report1.1G /home/arc/lizr/master/atf108K /home/arc/lizr/master/atf_pre20K /home/arc/lizr/master/atf_apk

现在可以知道是atf_runner和atf_report占用最多,接着可以用ls -l查看这两个文件夹下面有哪些文件


~$ ls -l /home/arc/lizr/master/atf_report/
总用量 24772drwxrwxr-x 4 arc arc 4096 37 01:45 20210307_014457514410drwxrwxr-x 4 arc arc 4096 37 01:47 20210307_014601203246drwxrwxr-x 4 arc arc 4096 37 01:48 20210307_014736618057drwxrwxr-x 4 arc arc 4096 37 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 cronarc 2533299 2533276 0 00:23 pts/1 00:00:00 grep --color=auto cronroot 2591934 1 0 207 ? 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   command0 0 * * * /home/script/timer_cache_clean.sh

这边定时配置的语法和Jenkins定时触发构建是一样的,只是在5个时间参数后面加上要执行的命令。

* * * * * <command>

之后保存修改,退出文件编辑,重启cron就配置完成了。

sudo service cron restart

关于sh脚本里面清理文件的配置可以参考上面find -exec操作。

访

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多