Raf
Raf

Reputation: 1767

How to get task name with maximum CPU usage in linux bash?

In linux, I'm writting a script to log system parameters to a file.

How can I get the name of the task consuming the most CPU resources, and the percentage of CPU used by that task?

For example, using top:

$ top -bin 1
top - 19:11:05 up  2:57,  1 user,  load average: 1,43, 1,47, 1,06
Tasks: 178 total,   2 running, 124 sleeping,   0 stopped,   0 zombie
%Cpu(s):  5,8 us,  1,3 sy,  0,0 ni, 92,8 id,  0,0 wa,  0,0 hi,  0,1 si,  0,0 st
KiB Mem :  3892704 total,  1594348 free,  1282992 used,  1015364 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  2335136 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
11883 root      20   0  645964 104036  87792 R  93,8  2,7  18:07.03 Xorg
12030 raf       20   0  412824  35632  14860 S  12,5  0,9   2:44.51 xfsettingsd
23468 raf       20   0   39648   3864   3332 R   6,2  0,1   0:00.02 top

From the exammple above, what I would like to have is a [sequence of [piped]] bash command[s] that outputs:

93.8   Xorg

Upvotes: 0

Views: 283

Answers (1)

ctac_
ctac_

Reputation: 2491

You can try

ps -eo %cpu,comm --sort %cpu | tail -n 1

Upvotes: 6

Related Questions