Register
It is currently Tue Oct 21, 2014 9:11 am

script: Kill process when it's using 100% time of CPU


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sun Jul 13, 2008 6:03 am   

Joined: Sun Jul 13, 2008 5:36 am
Posts: 1
Hello,

I'm looking for a monitoring script, which kill crashed processes. It's a big problem for me because when it happen they are using 100% time of 1 CPU (I've 2x Quad = 8 CPU in system). I think that the best solution will be simple bash script. If process is using 100% of CPU more than 10 seconds is killing by script. I'm working on Slackware 12.0. Any help would be much appreciated.



Thanks in advance.
Regards,
Patrick


Top
 Profile  
 PostPosted: Mon Jul 14, 2008 11:06 am   

Joined: Tue Apr 01, 2008 10:19 am
Posts: 49
I think that nagios provides this functionality, but perhaps it would be better to go from the other direction and try to prevent the process from hanging to begin with? Is it one in particular? Do you know what causes it?


Top
 Profile  
 PostPosted: Sun Sep 21, 2008 2:26 pm   

Joined: Sun Sep 21, 2008 2:22 pm
Posts: 1
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..


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP