Advertisement
Tomaskom

Skype killer

May 1st, 2013
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.45 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #Created by Tomaskom
  4.  
  5. #I created this because Linux version of skype often goes crazy
  6. #on login and eats all RAM available, pushing everything else
  7. #to swap and rendering the system unusable until oom-killer
  8. #does its job (which can take ages with large swap).
  9.  
  10. #script that kills KILLPROG when
  11. #CHECKPROG's RES memory exceeds LIMITMEM
  12.  
  13. #limit (MB) - minimal acceptable value is 10
  14. LIMITMEM="190"
  15.  
  16. #controlled program
  17. CHECKPROG=skype
  18. #program to be killed
  19. KILLPROG=skype
  20.  
  21. #put space before and $ after for exact process name only
  22. CHECK=`echo " ${CHECKPROG}$"`
  23.  
  24. echo $"Checking RAM usage by $CHECKPROG (limit ${LIMITMEM}m)"
  25. echo -n "Actual usage: "
  26.  
  27. while true;
  28. do
  29.     #strip top output to CHECKPROG RES memory
  30.     TOPMEM=`top -b -n 1 | grep "${CHECK}" | awk '{print $6}'`
  31.  
  32.     #rewrite last line with updated data
  33.     echo -en "\r\e[0K"
  34.     #no quotes here to get rid of newlines
  35.     echo -n Actual\ usage:\ $TOPMEM
  36.  
  37.     #process only if m suffix is in place
  38.     #(minimum 10m limit comes from here due to top output)
  39.     if TOPMEM=$(echo "$TOPMEM" | grep "m$"); then
  40.         #sum all instances of CHECKPROG
  41.         MEGAMEM=`echo "$TOPMEM" | awk '{s+=$1}END{print s}'`
  42.         #kill if MEGAMEM > LIMITMEM
  43.         if [[ "$MEGAMEM" -ge "$LIMITMEM" ]]; then
  44.             echo -e "\nkilling $CHECKPROG (for using $MEGAMEM MB)"
  45.             if killall -s SIGKILL $KILLPROG; then
  46.                 echo "$KILLPROG killed"
  47.                 exit
  48.             else
  49.                 echo "failed to kill $KILLPROG"
  50.                 exit
  51.             fi
  52.         fi
  53.     fi
  54. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement