过滤出内存使用率并进行判断
#!/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=80 echo mem_pused is $mem_pused% #clean mem_cache 使用bc来进行比较,不然会报错 if [ $(echo "$mem_pused > $percentage"|bc) -eq 1 ] then sync echo "sync执行成功" echo 1 > /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