Skip to content

Instantly share code, notes, and snippets.

@bscott
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save bscott/1b3875ee50da92dd7ce5 to your computer and use it in GitHub Desktop.

Select an option

Save bscott/1b3875ee50da92dd7ce5 to your computer and use it in GitHub Desktop.
CPU_Watcher
#!/bin/bash
while [ 1 ];
do
echo
echo checking for run-away process ...
CPU_USAGE=$(uptime | cut -d"," -f4 | cut -d":" -f2 | cut -d" " -f2 | sed -e "s/\.//g")
CPU_USAGE_THRESHOLD=900
PROCESS=$(ps -eLf)
TOPPROCESS=$(ps -eLf | grep "livemerge" | ps -eo pid -eo pcpu -eo command | sort -k 2 -r | grep -v PID | head -n 1)
if [ $CPU_USAGE -gt $CPU_USAGE_THRESHOLD ] ;
then
kill -9 $(ps -eLf | grep "livemerge" | ps -eo pid | sort -k 1 -r | grep -v PID | head -n 1) #original
kill -9 $(ps -eLf | grep "livemerge" | ps -eo pcpu | sort -k 1 -r | grep -v %CPU | head -n 1)
kill -9 $TOPPROCESS
echo system overloading!
echo Top-most process killed $TOPPROCESS
echo CPU USAGE is at $CPU_LOAD
else
echo "No Processes to kill"
fi
exit 0
sleep 1;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment