前言
一、功能
1.内存的使用率,超过70%给予提醒
二、代码
#!/bin/bash
#总的内存大小
mem_total=$(free -m|head -2|tail -1|awk '{print $2}')
#剩余的内存的大小
mem_free=$(free -m|head -2|tail -1|awk '{print $4}')
#剩余的百分比
num=$(echo "scale=2;$mem_free * 100 / $mem_total" | bc)
#使用的百分比
num2=$(echo "scale=2;100 - $num"|bc)
#比较的结果,是否使用超过70%
result=$(echo "$num > 30"|bc)
if (( $result == 1 ))
then
echo "内存还够用,使用率没有超过70%"
else
echo "内存不足,已经使用了$num2% 使用率超过70%,请关注系统的使用 "
fi
三、效果图
[root@localhost test]# vim mem_monitor.sh
[root@localhost test]# bash mem_monitor.sh
内存还够用,使用率没有超过70%
[root@localhost test]# free -m
total used free shared buff/cache available
Mem: 1819 231 1318 9 269 1436
Swap: 2047 0 2047
[root@localhost test]#