Linux之定时自动清理释放内存脚本

#!/bin/bash
mem_total=`free -m | awk 'NR==2' | awk '{print $2}'`
mem_free=`free -m | awk 'NR==3' | awk '{print $4}'`
mem_used=`free -m | grep Mem | awk '{print $3}'`
if (($mem_used!=0));then
    mem_per=0`echo "scale=2; $mem_free/$mem_total" | bc`
    DATA="$(date -d "today" + "%Y-%m-%d-%H-%M") free percent is:$mem_per"
    echo $DATA >> /tmp/mem_detect.log
    mem_warn=0.15
    mem_now=`expr $mem_per \> $mem_warn`
    if (($mem_now==0));then
        sync
        echo 1 > /proc/sys/vm/drop_caches
        echo 2 > /proc/sys/vm/drop_caches
        echo 3 > /proc/sys/vm/drop_caches
        echo "--->释放内存成功!!!" >> /tmp/memstat_$(date +%Y%m%d_%H%M%S).log
    fi
fi

*/5 * * * * /home/osoper/clean_caches_data.sh

忘了抄谁的了,暂时没有出处!只为记录,以便后期用到快速翻出

你可能感兴趣的:(Linux,运维)