20

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?

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
malfunctioning
  • 435
  • 1
  • 3
  • 10
  • If I do `top -d 5 | head -n 10` I get proper output, once. But if I do `watch -n 5 'top -d 5 | head -n 10'` then I get a lot of garbled output. It looks like `watch` doesn't handle `top` output properly. Any ideas? – malfunctioning Apr 24 '15 at 11:35

5 Answers5

22

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.

Kalki70
  • 467
  • 4
  • 11
  • 2
    I love this answer - but hit a small issue. Line buffering on grep meant if you use tee for ex to store the output, then it's not quite ideal. This works: " top -d 5 -b | stdbuf -o0 grep --line-buffered "load average" -A15 | tee output-file" hth – Goblinhack Feb 06 '18 at 20:41
  • 2
    I believe use "^top - " to replace "load average" is more general. If we use `-c` option to show the full COMMAND, there will be a `grep` process which with "load average" parameters, therefore the following line will also be `grep`ed out. This is unexpected. – Jason Pan Dec 22 '22 at 02:18
  • @JasonPan, good remark. – Kalki70 Jan 12 '23 at 22:59
17
> 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.

Alper
  • 12,860
  • 2
  • 31
  • 41
6

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

0

You can make config file for top (for example: run top command in interactive mode, then press "n" and write limit for number of processes, then press "W" to save this in your configuration file).

In the next step, you can run top in batch mode; parameter in config file limits output to requested value. So, then simple:

top -b > top.log

will be enough.

krzy-wa
  • 441
  • 1
  • 4
  • 5
0

The solution for MAC is :

top -a -n20 | awk 'FNR>=11 && FNR<=31{print $0};FNR==31{exit}' > cpustat.txt
Nathan SR
  • 29
  • 2