help in high memory usage alert script

can any one please help me to shell script

high memory usage alert

create a cronjob with this script and mail the result as you wish for yourself :

used=`free -m |awk 'NR==3 {print $3}'`
total=`free -m |awk 'NR==2 {print $2}'`
result=`echo "$used / $total" |bc -l`
result2=`echo "$result > 0.8" |bc`

if [ $result2 -eq 1 ];then
echo "more than 80% of ram used"
fi

i managed to write this script

#!/bin/bash
FREE=`free -m | awk '/^Mem:/ { printf( "%s\n", $4 ); }'`
USED=`free -m | awk '/^Mem:/ { printf( "%s\n", $3 ); }'`
TOTAL=`expr $FREE + $USED`
awk -v TOTAL=$TOTAL -v FREE=$FREE -v USED=$USED '
BEGIN {
printf "FREEPER: %3.2f%%\n", FREE / TOTAL * 100
printf "USEDPER: %3.2f%%\n", USED / TOTAL * 100
exit
}'

but the real problem is i would like to develop this like
to execute this in an intervals of 5 and if the used% exceeds specified value for consicutive n times and then echo a warning message

can you please do help me in this

use while and sleep :

#!/bin/bash
times=0

while true;do
used=`free -m |awk 'NR==3 {print $3}'`
total=`free -m |awk 'NR==2 {print $2}'`
result=`echo "$used / $total" |bc -l`
result2=`echo "$result > 0.8" |bc`

if [ $result2 -eq 1 ];then
        let times+=1
        if [ $times -gt 5 ];then
                echo "more than 80% of ram used"
        times=0
        fi
else
        times=0
fi

sleep 5
done
1 Like

this code is not working
root@h:/tmp# sh times.sh
times.sh: 18: let: not found

its because sh on ubuntu is dash not bash , you need to use :

bash times.sh
1 Like

haha lol yup thanks a lot dude