Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #Created by Tomaskom
- #I created this because Linux version of skype often goes crazy
- #on login and eats all RAM available, pushing everything else
- #to swap and rendering the system unusable until oom-killer
- #does its job (which can take ages with large swap).
- #script that kills KILLPROG when
- #CHECKPROG's RES memory exceeds LIMITMEM
- #limit (MB) - minimal acceptable value is 10
- LIMITMEM="190"
- #controlled program
- CHECKPROG=skype
- #program to be killed
- KILLPROG=skype
- #put space before and $ after for exact process name only
- CHECK=`echo " ${CHECKPROG}$"`
- echo $"Checking RAM usage by $CHECKPROG (limit ${LIMITMEM}m)"
- echo -n "Actual usage: "
- while true;
- do
- #strip top output to CHECKPROG RES memory
- TOPMEM=`top -b -n 1 | grep "${CHECK}" | awk '{print $6}'`
- #rewrite last line with updated data
- echo -en "\r\e[0K"
- #no quotes here to get rid of newlines
- echo -n Actual\ usage:\ $TOPMEM
- #process only if m suffix is in place
- #(minimum 10m limit comes from here due to top output)
- if TOPMEM=$(echo "$TOPMEM" | grep "m$"); then
- #sum all instances of CHECKPROG
- MEGAMEM=`echo "$TOPMEM" | awk '{s+=$1}END{print s}'`
- #kill if MEGAMEM > LIMITMEM
- if [[ "$MEGAMEM" -ge "$LIMITMEM" ]]; then
- echo -e "\nkilling $CHECKPROG (for using $MEGAMEM MB)"
- if killall -s SIGKILL $KILLPROG; then
- echo "$KILLPROG killed"
- exit
- else
- echo "failed to kill $KILLPROG"
- exit
- fi
- fi
- fi
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement