Ubuntu – How to automatically kill a process when it’s memory use exceeds a specific limit

command lineskype

Sometimes when i run skype it takes an enormous amount of memory so much in fact
that i can't close it and everything just lags like CRAZY. But this is only sometimes
is there a command that can track skype and when it's taking a HUGE amount of memory kill it. something like this

track skype when memory exceeds XXXX killall skype

Best Answer

While there could be a point in setting limits proactively, let's answer OP's question :-)

Make an Upstart script in /etc/init/skype-killer.conf. Replace skype with Skype's binary name if its not skype, and replace 500000 (as in 500000 kB = 500 M) with the limit in kB's you wish to enforce for Skype virtual memory size and 30 with the number of seconds between checks.

This job will start on startup and keep running checking for skype memory usage. After you have copied this to /etc/init with sudo, do sudo start skype-killer to get it going, or reboot system. If you wish to revise memory usage, edit the skype-killer.conf and do sudo stop skype-killer; sudo start skype-killer.

start on startup
stop on shutdown

script
  while /bin/true; do
    if $(pidof skype); then
      test $(cat /proc/`pidof skype`/status | grep "VmSize:" | sed -re s,VmSize:\\s+,, | cut -d" " -f1) -gt 500000 && killall skype
    fi
    sleep 30
  done
end script