How to get all process ids with memory usage great

2019-09-28 12:30发布

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

标签: linux bash unix sh ps
2条回答
等我变得足够好
2楼-- · 2019-09-28 12:52

This following command will help I think,

ps aux --sort -rss

Try it.

查看更多
叼着烟拽天下
3楼-- · 2019-09-28 13:03

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.

查看更多
登录 后发表回答