2020-09-23

一、内存判断清理脚本(ClearFree.sh)

#!/bin/bash

echo "current time is $(date -d "today" +"%Y-%m-%d-%H-%M-%S")" >>/opt/package/logs/mem_auto_$(date +%Y%m%d).log

#系统分配的区总量

mem_total=`free | grep "Mem:" |awk '{print $2}'`

echo "mem_total is $mem_total " >>/opt/package/logs/mem_auto_$(date +%Y%m%d).log

#当前剩余的大小

mem_free=`free | grep 'buffers/cache' | awk '{print $3}'`

 

echo "mem_free is $mem_free" >>/opt/package/logs/mem_auto_$(date +%Y%m%d).log

#当前已使用的used大小

mem_used=`free -m | grep Mem | awk '{print $3}'`

echo "mem_used is $mem_used" >>/opt/package/logs/mem_auto_$(date +%Y%m%d).log

if (($mem_used != 0)); then

 

#如果已被使用,则计算当前剩余free所占总量的百分比,用小数来表示,要在小数点前面补一个整数位0

mem_per=0`echo "scale=2;$mem_free/$mem_total" | bc`

echo "free percent is $mem_per" >>/opt/package/logs/mem_auto_$(date +%Y%m%d).log

DATA="$(date -d "today" +"%Y-%m-%d-%H-%M-%S") free percent is : $mem_per"

echo $DATA >> /opt/package/logs/mem_auto_$(date +%Y%m%d).log

echo $DATA >> /opt/package/logs/mem_detect.log

#设置的告警值为44%(即使用超过56%的时候告警)。

mem_warn=0.4

echo "mem_warn is $mem_warn" >>/opt/package/logs/mem_auto_$(date +%Y%m%d).log

#当前剩余百分比与告警值进行比较(当大于告警值(即剩余44%以上)时会返回1,小于(即剩余不足44%)时会返回0 )

mem_now=`expr $mem_per \> $mem_warn`

echo "剩余百分比与警告值比较 mem_now is $mem_now" >>/opt/package/logs/mem_auto_$(date +%Y%m%d).log

echo "when mem_now is 1 , means mem is ok ! " >>/opt/package/logs/mem_auto_$(date +%Y%m%d).log

 

echo "-----------------------------------" >>/opt/package/logs/mem_auto_$(date +%Y%m%d).log

#如果当前使用超过56%(即剩余小于44%,上面的返回值等于0),释放内存

if (($mem_now == 0)); then

echo "but now the mem_now is 0 ,小于(即内存剩余不足44%),所以清理内存, start to clear memery....." >>/opt/package/logs/mem_auto_$(date +%Y%m%d).log

sync

echo 1 > /proc/sys/vm/drop_caches

echo 2 > /proc/sys/vm/drop_caches

echo 3 > /proc/sys/vm/drop_caches

echo "---> start auto clear memery is OK ! $DATA , warn is $mem_warn ,小于(即内存剩余不足44%),所以清理内存, " >>/opt/package/logs/mem_auto_$(date +%Y%m%d_%H).log

 

fi

 

 

#取当前空闲cpu百份比值(只取整数部分)

cpu_idle=`top -b -n 1 | grep Cpu | awk '{print $5}' | cut -f 1 -d "."`

echo "cpu_idle percent is $cpu_idle,cpu 剩余量充足,警告值是当剩余不足30%时,自动清理内" >>/opt/package/logs/mem_auto_$(date +%Y%m%d).log

echo "================================================================" >>/opt/package/logs/mem_auto_$(date +%Y%m%d).log

#设置空闲cpu的告警值为30%,如果当前cpu使用超过70%(即剩余小于30%),立即发邮件告警,自动清理内存

if (($cpu_idle < 30)); then

echo " cpu 剩余不足30% ,所以清理内存, start to clear memery....." >>/opt/package/logs/mem_auto_$(date +%Y%m%d).log

sync

echo 1 > /proc/sys/vm/drop_caches

echo 2 > /proc/sys/vm/drop_caches

echo 3 > /proc/sys/vm/drop_caches

echo "--->cpu used more than 70% ,so start auto clear memery is OK ! $DATA , warn is $mem_warn " >>/opt/package/logs/memstat_cpu_auto_$(date +%Y%m%d_%H).log

 

fi

fi

 

设置定时任务

crontab -e

10 00 * * * sh /opt/package/ClearFree.sh

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

打赏
文章很值,打赏犒劳作者一下
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页

打赏

h_搬运工

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者