Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit top command to only display top X processes on command line

I'm not sure why there is not an option in the top command that does this, as it seems to be a natural request.

If I pipe the output of top to head, then the list doesn't update and I get static output once. I could then bring the watch command into action, which would do the job. But, is there a simpler solution?

like image 884
malfunctioning Avatar asked Apr 24 '15 10:04

malfunctioning


People also ask

How do you filter specific process in top command?

To filter the top output to a specific process, press the O key and enter the entry as COMMAND=name, where the name refers to the process name. Press ENTER, and the top utility will filter the processes to systemd only. You can also highlight the specific process while keeping other processes in view.

How do I run a top command only once?

notice that if you would like to keep "top" command for a script, you could use -b option ("-b" option means "batch mode operation"), this option allows you to redirect the output into a file. Examples: top one shoot batch mode : top -bn 1 2>&1 1> /tmp/topN.


4 Answers

I use a trick, specially for batch mode. I pipeline the exit to grep, with option "-A", to show N lines after match.

As in the first line of top there is something like: "load average", I grep that, for instance:

$ top -d 5 -b|grep "load average" -A 15
top - 09:42:34 up 38 min,  1 user,  load average: 0.22, 0.39, 0.53
Tasks: 294 total,   2 running, 291 sleeping,   0 stopped,   1 zombie
%Cpu(s):  3.5 us,  0.9 sy,  0.0 ni, 94.6 id,  0.5 wa,  0.3 hi,  0.1 si,  0.0 st
KiB Mem :  8065144 total,  2213800 free,  2733524 used,  3117820 buff/cache
KiB Swap: 24575996 total, 24575996 free,        0 used.  4613128 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 2744 lrojas    20   0 3376820 752000 116588 R  20.2  9.3   9:30.01 firefox
 1869 lrojas     9 -11  566164  18336  14300 S   5.2  0.2   2:35.78 pulseaudio
 2401 lrojas    20   0  740092 200456  87256 S   2.4  2.5   0:57.29 skype
 2402 lrojas    20   0  617872 172924  76172 S   2.2  2.1   0:57.17 skype
 1333 root      20   0  459028  60992  48024 S   1.6  0.8   0:36.14 Xorg
 1838 lrojas    20   0 2103336 184468  64724 S   1.4  2.3   0:56.85 gnome-shell
 2359 lrojas    20   0  741212  35068  24620 S   1.4  0.4   0:06.83 gnome-terminal-
 2404 lrojas    20   0 1867556 229912  83988 S   0.8  2.9   0:19.63 thunderbird
 1249 apache    20   0  461436  10196   3404 S   0.4  0.1   0:00.57 httpd

This way it will continue in batch mode, always showing only the first N lines of output.

Completely standard solution, for any version of top.

like image 134
Luis Rojas Avatar answered Oct 14 '22 01:10

Luis Rojas


> top

then, press n to set maximum tasks displayed.

When operating top, one of the most important key is help (h or ?) to see the available options (n is given in help).

UPDATE (after the the comment):

PERSONAL Configuration File might help for the batch mode. Run top then set the maximum tasks displayed with n and use the W interactive command to create or update the configuration file. top will be ran according to the configuration file next time.

like image 30
Alper Avatar answered Oct 16 '22 15:10

Alper


I use a trick, specially for batch mode. I pipeline the exit to grep, with option "-A", to show N lines after match.

As in the first line of top there is something like: "load average", I grep that, for instance:

$ top -d 5 -b|grep "load average" -A 15
top - 09:42:34 up 38 min,  1 user,  load average: 0.22, 0.39, 0.53
Tasks: 294 total,   2 running, 291 sleeping,   0 stopped,   1 zombie
%Cpu(s):  3.5 us,  0.9 sy,  0.0 ni, 94.6 id,  0.5 wa,  0.3 hi,  0.1 si,  0.0 st
KiB Mem :  8065144 total,  2213800 free,  2733524 used,  3117820 buff/cache
KiB Swap: 24575996 total, 24575996 free,        0 used.  4613128 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 2744 lrojas    20   0 3376820 752000 116588 R  20.2  9.3   9:30.01 firefox
 1869 lrojas     9 -11  566164  18336  14300 S   5.2  0.2   2:35.78 pulseaudio
 2401 lrojas    20   0  740092 200456  87256 S   2.4  2.5   0:57.29 skype
 2402 lrojas    20   0  617872 172924  76172 S   2.2  2.1   0:57.17 skype
 1333 root      20   0  459028  60992  48024 S   1.6  0.8   0:36.14 Xorg
 1838 lrojas    20   0 2103336 184468  64724 S   1.4  2.3   0:56.85 gnome-shell
 2359 lrojas    20   0  741212  35068  24620 S   1.4  0.4   0:06.83 gnome-terminal-
 2404 lrojas    20   0 1867556 229912  83988 S   0.8  2.9   0:19.63 thunderbird
 1249 apache    20   0  461436  10196   3404 S   0.4  0.1   0:00.57 httpd

This way it will continue in batch mode, always showing only the first N lines of output.

Completely standard solution, for any version of top.

like image 20
Kalki70 Avatar answered Oct 16 '22 15:10

Kalki70


Perhaps you should add the -b parameter which runs top in the batch mode: watch -n 5 'top -b -d 5 | head -n 10'

like image 5
Sergey Emantayev Avatar answered Oct 16 '22 14:10

Sergey Emantayev