1

I have installed a program called earlyoom which kills the most RAM intensive when my RAM and Swap is almost full. However, I realized that my laptop is constantly freezing due to CPU usage being at 100% on all four virtual cores(2 cores with hyperthreading. Is there any program or script that kills the most CPU intensive program when the CPU is at 100%?

1 Answer 1

0

I found this which seems to address your needs. I haven't tested it ... you should be able to adapt it for your purposes. Of course, indiscriminately killing random processes automatically may be inadvisable.

#!/bin/bash

## Note: will kill the top-most process if the $CPU_LOAD is greater than the $CPU_THRESHOLD.
echo
echo checking for run-away process ...

CPU_LOAD=$(uptime | cut -d"," -f4 | cut -d":" -f2 | cut -d" " -f2 | sed -e "s/\.//g")
CPU_THRESHOLD=300
PROCESS=$(ps aux r)
TOPPROCESS=$(ps -eo pid -eo pcpu -eo command | sort -k 2 -r | grep -v PID | head -n 1)

if [ $CPU_LOAD -gt $CPU_THRESHOLD ] ; then
  # kill -9 $(ps -eo pid | sort -k 1 -r | grep -v PID | head -n 1) #original
  # kill -9 $(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 load average is at $CPU_LOAD
  echo 
  echo Active processes...
  ps aux r

  # send an email using mail
  SUBJECT="Runaway Process Report"
  # Email To ?
  EMAIL="[email protected]"
  # Email text/message
  EMAILMESSAGE="/tmp/emailmessage.txt"
  echo "System overloading, possible runaway process."> $EMAILMESSAGE
  echo "Top-most process killed $TOPPROCESS" >>$EMAILMESSAGE
  echo "Load average was at $CPU_LOAD" >>$EMAILMESSAGE
  echo "Active processes..." >>$EMAILMESSAGE
  echo "$PROCESS" >>$EMAILMESSAGE
  mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE

else
 echo
 echo no run-aways. 
 echo load average is at $CPU_LOAD
 echo 
 echo Active processes...
 ps aux r
fi
exit 0
2
  • How do I calculate te CPU threshold?
    – user669400
    Jun 22, 2017 at 1:23
  • $CPU_THRESHOLD would depend on the number of (CPU) cores you have on your system. You'll have to experiment a little to find a threshold value that meets your needs.
    – jones0610
    Jun 22, 2017 at 1:28

You must log in to answer this question.