Skip to content

Instantly share code, notes, and snippets.

@hpez
Created December 27, 2018 12:52
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 hpez/e0e552052d2ebbd83792fcf6090732ca to your computer and use it in GitHub Desktop.
Save hpez/e0e552052d2ebbd83792fcf6090732ca to your computer and use it in GitHub Desktop.
A gist to kill processes taking more than i% cpu. Useful for closing huge processes really fast and rescuing!
#!/bin/bash
output="$(sudo ps -eo %cpu,pid --sort -%cpu)"
while read -r line; do
words=($line)
st=$(echo ${words[0]}'>'5 | bc -l) # Change the number '5' to any number kill the processes taking more than that percent of cpu
if [ "$st" = "1" ];
then
echo ${line}
exec sudo kill -9 ${words[1]}
fi
done <<< "$output"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment