#!/bin/bash
used=`free -m | awk 'NR==2' | awk '{print $3}'`
free=`free -m | awk 'NR==2' | awk '{print $4}'`
Buff=`free -m | awk 'NR==2' | awk '{print $6}'`
#if [!-d "/usr/local/alarmAgent/log/drop_caches.log" ];then
#touch "/usr/local/alarmAgent/log/drop_caches.log"
#echo "文件创建成功" >> /usr/local/alarmAgent/log/drop_caches.log
#fi
echo "开始清除缓存" >> /usr/local/alarmAgent/log/drop_caches.log
date >> /usr/local/alarmAgent/log/drop_caches.log
echo "Memory usage before | [ Used:${used}MB ] [ Free:${free}MB ] [ Buff:${Buff}MB ]" >> /usr/local/alarmAgent/log/drop_caches.log
if [ $Buff -ge 500 ];then
#清除页面缓存
sync && echo 1 > /proc/sys/vm/drop_caches
#清除目录项和inode
sync && echo 2 > /proc/sys/vm/drop_caches
#sync && echo 3 > /proc/sys/vm/drop_caches
used2=`free -m | awk 'NR==2' | awk '{print $3}'`
free2=`free -m | awk 'NR==2' | awk '{print $4}'`
Buff2=`free -m | awk 'NR==2' | awk '{print $6}'`
echo "Memory usage after | [ Used:${used2}MB ] [ Free:${free2}MB ] [ Buff2:${Buff2}MB ]" >> /usr/local/alarmAgent/log/drop_caches.log
echo "ok" >> /usr/local/alarmAgent/log/drop_caches.log
else
echo "Not Required" >> /usr/local/alarmAgent/log/drop_caches.log
fi
exit 1
11-16
09-25
562
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)