How to get all process ids with memory usage greater than

1.7k views Asked by At

I need to get all process ids which have memory usage greater or lower than predifined number. For example get id where memory (rss) usage grater than 10MB and then using this id kill each process. Thanks

2

There are 2 answers

0
Arnab Nandy On

This following command will help I think,

ps aux --sort -rss

Try it.

0
Alfe On

That's not a good idea. You will certainly kill processes you should not and might render your system damaged in the process. But anyway, here's what does the trick:

ps -eo rss=,pid=,user=,comm= k -rss |
  while read size pid user comm
  do
    [ "$user" = "alfe" ] || continue  # adjust user name here
    if [ "$size" -gt 10000 ]
    then
      echo "kill $pid # $size $user $comm"
    else
      break
    fi
  done

You might want to replace the echo line by a line using kill directly, but as I said, this will probably kill processes you should not kill.

The line with the continue is meant to skip all processes which are not of a specific user; I just assumed that; if you intend to run this as root, feel free to remove that line.