// article · linux
CentOS7 内存缓存占用过大,设置定时清除 buff/cache 的脚本
Linux系统buff/cache 中缓存数据占用内存过高,定时清理buff/cache ,释放系统内存
用root用户创建定时任务
切换到root用户
su - root创建脚本文件
touch cleanCache.sh编辑脚本文件
vim cleanCache.sh添加如下内容:
#!/bin/bash#每两小时清除一次缓存
echo "开始清除缓存"
sync;sync;sync #写入硬盘,防止数据丢失
sleep 10#延迟10秒
echo 3 > /proc/sys/vm/drop_caches创建定时任务
crontab -e //弹出配置文件添加如下内容:(每2小时执行一次,按需修改)
0 */2 * * * ./cleanCache.sh保证crond启动以及开机自启
systemctl start crond.service
systemctl enable crond.service查看buff/cache情况
free -m查看定时任务是否被执行
cat /var/log/cron | grep cleanCache