How to display in the user terminal, the most loaded process?

I tried this: ps -eo %u | sort -nr -k 1| head -1 ps -eo %u | sort -nr -k 1| head -1 ps -eo %u | sort -nr -k 1| head -1 Displays the largest (by number) user Pid.

Tell me how to display exactly the user. For example, there is the highest user vasya process in which mem or cpu is 70% loaded.

    2 answers 2

    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.
    • Thank you very much! Thanks you! - KaruseL

    Here is the cross-platform version of Python:

     #!/usr/bin/env python import time import psutil for process in psutil.process_iter(): process.cpu_percent() # ignore time.sleep(1) # averaging interval in seconds print(max(psutil.process_iter(), key=psutil.Process.cpu_percent).username()) 

    Script:

    1. cpu_percent() all running processes and calls cpu_percent() to indicate the time from which statistics will be considered (the return value of the first call is meaningless)
    2. sleeps 1 second - the time by which CPU utilization is averaged for each process. Correct for your needs.
    3. finds the process with the highest CPU share in the elapsed time (one second) and prints the corresponding user name.

    To work you need to put a psutil dependency, for example, on Ubuntu:

     $ sudo apt-get install python-psutil 

    Or if you do not want to touch the system, then only for the current user you can put:

     $ pip install --user psutil 

    See How to run a python file from anywhere in Ubuntu?