How to get all process ids with memory usage greater than

北战南征 提交于 2019-12-13 11:26:45

问题


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


回答1:


This following command will help I think,

ps aux --sort -rss

Try it.




回答2:


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.



来源:https://stackoverflow.com/questions/26862748/how-to-get-all-process-ids-with-memory-usage-greater-than

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!