linux 过滤内存使用率并于计划任务结合来自动清理内存缓存

时间:2023-12-20 09:38:14

过滤出内存使用率并进行判断

#!/bin/bash
echo "###cleand free_cache script########"
#memory usage
mem_pused=`/usr/bin/free -m | sed -n '2p'|awk '{ print ($3+$6)/$2*100}'`
#memory usage percentage
percentage=
echo mem_pused is $mem_pused%
#clean mem_cache 使用bc来进行比较,不然会报错
if [ $(echo "$mem_pused > $percentage"|bc) -eq ]
then
sync
echo "sync执行成功"
echo > /proc/sys/vm/drop_caches
Current_memory_utilization=`/usr/bin/free -m | sed -n '2p'|awk '{ print ($3+$6)/$2*100}'`
echo "Current_memory_utilization:$Current_memory_utilization%"
else
echo "memory not clean ====== exit script"
fi