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
Asked
Active
Viewed 1,583 times
-4
-
3OK! Show what you did so far and what is not working, so we can try to help you. If it is your homework, start by trying to do it ;) – fedorqui Nov 11 '14 at 10:32
-
1Hint : Try to get the list of process running – Santosh A Nov 11 '14 at 10:33
-
Use `top` or `ps auxw` or `pgrep` ; perhaps pipe its output to `awk` – Basile Starynkevitch Nov 11 '14 at 10:33
-
If this is homework, then the teacher is a true BOFH :-)) – Alfe Nov 11 '14 at 11:05
-
Also see [How to see top processes sorted by actual memory usage?](https://stackoverflow.com/q/4802481/608639) and [Check memory per processes and subprocesses](https://stackoverflow.com/q/25495619/608639), [Script to get user that has process with most memory usage?](https://stackoverflow.com/q/41177409/608639), [A way to determine a process's “real” memory usage, i.e. private dirty RSS?](https://stackoverflow.com/q/118307/608639), etc – jww Apr 29 '18 at 21:17
2 Answers
0
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.

Alfe
- 56,346
- 20
- 107
- 159