Skip to content

Instantly share code, notes, and snippets.

@MarcoGriep88
Created January 27, 2022 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcoGriep88/90506c5724e2180e4857c898eb02d8ee to your computer and use it in GitHub Desktop.
Save MarcoGriep88/90506c5724e2180e4857c898eb02d8ee to your computer and use it in GitHub Desktop.
Shell Script - Kill Process if CPU usage gets to high for a given time
#!/bin/bash
# ----------------------------------------------------------
# Author: Griep Marco, www.marcogriep.de
# License:
# You can edit, distribute or sell this snippet if you want,
# but please contribute to my webpage www.marcogriep.com
#
# If you use this Script commercially, please send a dollar
# to "griepmarco@gmail.com" via paypal. (Pay-What-You-Want)
# ----------------------------------------------------------
# ----------------------
# Ajust for your needs :)
# ----------------------
_PROCESS_NAME=firefox #could be kd001 or something like this
_CPU_THRESHOLD=0.5 #Threshold in GhZ -> 0.2 = 500 mhz -> Will be killed after its still to high after x minutes
_CPU_THRESHOLD_CRIT=0.7 #Threshold in GhZ -> 0.7 = 700 mhz -> Will be killed immediatly
_MAX_WAIT=4 #Iterations. Check how often the CPU is to high. Bsp: A Value of 4 Needs 4 Rounds a 1 minute. 4 Minutes + First run = 5 Minutes. After 5 Minutes CPU is still to high, process will be killed
# -----------------
# DO NOT EDIT!
# Instance Variables
# -----------------
_CPU_LOAD=0
_PID=-1
_CPU_HIGH_FOR=0
#Endless Loop
while [ true ]
do
#Search Process
myProcesses=$(ps ax -o pid,pcpu,command | grep $_PROCESS_NAME)
processList=$(echo $myProcesses | tr " " "\n")
counter=1;
for p in $processList
do
#Find Process ID
if [ $counter -eq 1 ] ; then
_PID=[$p]
stringLengh=${#_PID}
stringLengh=$((stringLengh-1));
_PID=$(echo $_PID | cut -c 2-$stringLengh)
fi
#Find CPU Load of current Process
if [ $counter -eq 2 ] ; then
_CPU_LOAD=[$p]
stringLengh=${#_CPU_LOAD}
stringLengh=$((stringLengh-1));
_CPU_LOAD=$(echo $_CPU_LOAD | cut -c 2-$stringLengh)
break; #if everything found, stop iteration
fi
counter=$((counter+1));
done
#Check if CPU Load of Process is more than Critical Threshold
if [ $(bc <<< "$_CPU_LOAD >= $_CPU_THRESHOLD_CRIT") -eq 1 ]; then
echo "CRITICAL-KILL: CPU usage is over critical threshold. Instant killing process...$_PID by CPU $_CPU_LOAD";
kill -9 $_PID;
$_PID=-1;
else
#If CPU Use is not Critical but to high.
if [ $(bc <<< "$_CPU_LOAD >= $_CPU_THRESHOLD") -eq 1 ]; then
echo "INFO: Found PID greater than Threshold...$_PID by CPU $_CPU_LOAD";
#Check if after x Minutes, Process is still to high
if [ $(bc <<< "$_CPU_HIGH_FOR >= $_MAX_WAIT") -eq 1 ]; then
kill -9 $_PID
$_PID=-1;
_CPU_HIGH_FOR=0;
echo "KILL: PID has to much CPU usage even after timeout. Killing PID... $_PID by CPU $_CPU_LOAD";
else
_CPU_HIGH_FOR=$((_CPU_HIGH_FOR+1));
echo "WARNING: CPU is over threshold, waiting until timeout reached...$_PID by CPU $_CPU_LOAD";
fi
else
echo "INFO: Process is not over Threshold....succeed checking runtime...$_PID by CPU $_CPU_LOAD";
fi
fi
#Wait a minute after next run
sleep 1m
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment