Advertisement
adamundefined

autoKillChromeProcess.sh

Aug 10th, 2022
697
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.04 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. now=$(date)
  4. threshold=98
  5. parentProcess=$(ps -eo pid,cmd --sort=+pid | grep chrom | head -1 | tr -s ' ' | cut -f 2 -d ' ')
  6. topMemoryProcess=$(ps -eo pid,cmd,%mem --sort=-%mem | grep chrom | head -1 | tr -s ' ' | cut -f 2 -d ' ')
  7. secondTopMemoryProcess=$(ps -eo pid,cmd,%mem --sort=-%mem | grep chrom | head -2 | sort -nr | head -1 | tr -s ' ' | cut -f 2 -d ' ')
  8. realmem=$(free | awk 'FNR == 2 {print $4/($3+$4)*100-100}'| sed 's/-//g' |cut -d "." -f1)
  9.  
  10. #echo "Parent:"$parentProcess
  11. #echo "Top:"$topMemoryProcess
  12. #echo "Second:"$secondTopMemoryProcess
  13.  
  14. if [ "$realmem" -gt "$threshold" ]
  15. then
  16.     if [ $parentProcess = $topMemoryProcess ]
  17.     then
  18.         #echo "Parent is top"
  19.         echo "Killing process: " $secondTopMemoryProcess
  20.         kill -9 $secondTopMemoryProcess
  21.     else
  22.         #echo "Parent is NOT top"
  23.         echo "Kill: " $topMemoryProcess
  24.         kill -9 $topMemoryProcess
  25.     fi
  26.     notify-send "Memory alert; Chrome process killed"
  27. else
  28.     echo "$now" "realmem:" $realmem
  29.     #notify-send "Memory test"
  30. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement