Rock笔记
~/rocknotes/articles/286.md article
// 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


回复// 0 replies

最新回复 ↓
// 暂无回复

回复功能沿用原详情页:前往原页面参与讨论 →