Last active
January 22, 2018 00:39
-
-
Save lmangani/0da22055fbb7cfb123196869d2c089ce to your computer and use it in GitHub Desktop.
Kill CPU hogs from CRON - a poor man's Monit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# tries to kill process with highest CPU load | |
# (if it is part of a specified list of troublemakers) | |
MAXLOAD=5 | |
TROUBLEMAKERS="chrome" | |
sleep 1 # wait a few seconds (just as a precaution) | |
TOPPROCESS=$(top -b -n 1 | sed 1,6d | sed -n 2p) | |
TOPPID=$(echo "$TOPPROCESS" | awk '{print $1}') | |
TOPNAME=$(echo "$TOPPROCESS" | awk '{print $12}') | |
LOAD=$(uptime | awk '{print $10}' | cut -d "," -f 1) | |
if [[ "$TROUBLEMAKERS" == *"$TOPNAME"* ]]; then | |
if [[ "$LOAD" -gt "$MAXLOAD" ]] | |
then | |
echo "Cause of high CPU load: "$TOPNAME" ("$TOPPID")" | |
echo "In troublemaker list. Killing..." | |
kill -9 $TOPPID | |
else | |
echo "Cause of high CPU load: "$TOPNAME" ("$TOPPID")" | |
echo "Not above MAXCPU threshold. Exiting..." | |
exit 1 | |
fi | |
else | |
echo "Cause of high CPU load: "$TOPNAME" ("$TOPPID")" | |
echo "Not in troublemaker list. Exiting..." | |
exit 1 | |
fi | |
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# tries to kill process with highest CPU load | |
# (if it is part of a specified list of troublemakers) | |
MAXLOAD=5 | |
TROUBLEMAKERS="chrome gnome-keyring-daemon" | |
sleep 1 # wait a few seconds (just as a precaution) | |
TOPPROCESS=$(top -b -n 1 | sed 1,6d | sed -n 2p) | |
TOPPID=$(echo "$TOPPROCESS" | awk '{print $1}') | |
TOPNAME=$(echo "$TOPPROCESS" | awk '{print $12}') | |
LOAD=$(uptime | awk '{print $10}' | cut -d "," -f 1) | |
if [[ "$TROUBLEMAKERS" == *"$TOPNAME"* ]]; then | |
echo "$LOAD, $MAXLOAD" | |
if [[ ${LOAD#0} -gt ${MAXLOAD#0} ]]; then | |
echo "Cause of high CPU load: $TOPNAME ($TOPPID)" | |
echo "Killing..." | |
kill -9 $TOPPID | |
fi | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment