L
L
lergus2017-01-24 11:44:15
linux
lergus, 2017-01-24 11:44:15

How to calculate the total amount of memory occupied by user processes in the terminal?

Actually I can't figure out how to work with tables and arrays from the terminal.
I have debian 8 with xfce and I want to see which user has eaten the most memory.
Tell me where to dig?
I got something terrible.

function kill_user {
rm ~/user.txt
  for item in `ps -eo user,rss,cmd | grep X11rdp |awk '{print($1)}'` #отбираем пользователей
  do
echo `ps -eo user,rss,cmd | grep $item |awk '{ SUM += $2 } END { print SUM }'` $item >> ~/user.txt #Считаем память по каждому
done
#printf $user
mem_user=`cat ~/user.txt | sort -rg | head -n1 | awk '{print($2)}'` #Сортируем и оставляем самого прожорливого
#echo "Память жрет $mem_user - он будет наказан"
kill `ps -eo pid,user,cmd | grep X11rdp |grep $mem_user |awk '{print($1)}'` #Убиваем
}

if ((`free | awk '/Mem/  {print$4}'` <  700000)); #При проблемах с памятью убиваем самых прожорливых
then 
kill_user
#else
#echo "пока рано"
fi

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Gintoki, 2017-02-02
@Gintoki

You can do something like this:
This simple combination will show the total RAM usage by a particular user.
Then you can write a simple script that would go through all the users and write the result to a file for later parsing.
PS. There is at least one "but!". If the required uid is less than a hundred (some system user), then grep can give an error (perhaps even a strong one) when sampling.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question