The question is interesting.
If, of course, I understood him correctly, and the answer does not boil down to a simple launch
top
(and, if necessary, switch sorting with the M key).
Let's first deal with the memory
it is easier:
$ ps axh -o user --sort -vsize | head -n 1 tomcat
This command will print the name of the user who uses the most memory (in this case, it turned out to be tomcat ). If you want more details, add columns: -o user,vsize,cmd .
Keys:
ax - show all processes.h - do not show headlines.-o user - output format.--sort -vsize - sorting by memory in descending order (descending is specified by a sign - before vsize ).
Now interesting - CPU
I would like by analogy to use ps --sort cpu . However, the man says:
CPU usage is currently expressed as the percentage of time spent running during the lifetime of a process. This is not ideal,
and this truth is not what we need, we need to understand which process is loading the most now .
And what about the top command? She has batch mode, it would seem, top -b -n 1 | head top -b -n 1 | head and problem solved. However, practice shows that with this launch top shows far from the most accurate information. For example, the hardest process he can show himself.
To make the data more reliable, top needs some time to work.
And here comes the decision. Run the top in several iterations, and take the hardest process out of the last one. Call on awk help.
Awk script:
max-user.awk
# инициализация BEGIN { START=999 ; COUNT = 0 } # Как только видим Tasks в начале строки, понимаем, # что top выодит новую порцию данных. # Первая строка с процессом находится на 6 строк дальше. /^Tasks/ { START = NR + 6 } # 7-я строка - это заголовок. Выведем для красоты. NR == 7 { print } # Наконец-то строка с нашим процессом. Посчитаем итерацию # и выведем текущее состояние. NR == START { COUNT++; print } # Прошло 3 итерации, достаточно. COUNT == 3 { exit }
Run like this:
$ top -b -n 10 -d 1 | awk -f max-user.awk PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 16235 cronfy 20 0 43216 3828 3196 R 12,5 0,1 0:00.02 top 3070 olenka 20 0 4640760 997152 39036 S 3,0 13,1 24:59.97 java 5099 olenka 20 0 859700 135380 70324 S 2,9 1,8 10:45.65 chrome
In the last line (not in the first!) We see the most loading process. You can get the username like this:
$ top -b -n 10 -d 1 | awk -f max-user.awk | awk 'END { print $2 }' root
Top keys:
-b - non-interactive mode.-n 10 is the number of iterations. You can put a lot, in any case, the execution will interrupt awk.-d 1 - the delay between iterations.