Hello there,
This script does exactly what you want, all you have to do is modify a few things for your needs.
Code:
#!/bin/bash
while true
do
sleep 60
USAGE=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{ print $1 } '`
USAGE=${USAGE%%.*}
PID=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{print $2 }'`
PNAME=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{print $3 }'`
done
if [ $USAGE -gt 95 ]
then
USAGE1=$USAGE
PID1=$PID
PNAME1=$PNAME
sleep 7
USAGE2=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{ print $1 } '`
USAGE2=${USAGE2%%.*}
PID2=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{print $2 }'`
PNAME2=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{print $3 }'`
# 2nd Check after 7 sec.
[ $USAGE2 -gt 95 ] && [ $PID1 = $PID2 ] && mail -s "WEB: CPU load from process $PNAME has reached 95%" admin@host.com < .
fi
done
As you see, the last line sends an email when a proccess reaches 95%, and that only if this proccess uses 95% still after 7 seconds of the first check.You can modify this line instead to email, kill the proccess. maybe kill $PID ?
I hope it helps
just my 2 cents..