11

I wish to suppress the general information for the top command using a top parameter.

By general information I mean the below stuff :

top - 09:35:05 up  3:26,  2 users,  load average: 0.29, 0.22, 0.21
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
Cpu(s):  2.3%us,  0.7%sy,  0.0%ni, 96.3%id,  0.8%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   3840932k total,  2687880k used,  1153052k free,    88380k buffers
Swap:  3998716k total,        0k used,  3998716k free,   987076k cached

What I do not wish to do is :

top -u user | grep process_name

or

top -bp $(pgrep process_name) | do_something

How can I achieve this?

Note: I am on Ubuntu 12.04 and top version is 3.2.8.

Laurel
  • 5,965
  • 14
  • 31
  • 57
sjsam
  • 21,411
  • 5
  • 55
  • 102

7 Answers7

10

Came across this question today. I have a potential solution - create a top configuration file from inside top's interactive mode when the summary area is disabled. Since this file is also read at startup of top in batch mode, it will cause the summary area to be disabled in batch mode too.

Follow these steps to set it up..

  1. Launch top in interactive mode.

  2. Once inside interactive mode, disable the summary area by successively pressing 'l', 'm' and 't'.

  3. Press 'W' (upper case) to write your top configuration file (normally, ~/.toprc)

  4. Exit interactive mode.

Now when you run top in batch mode the summary area will not appear (!)

Taking it one step further...

If you only want this for certain situations and still want the summary area most of the time, you could use an alternate top configuration file. However, AFAIK, the way to get top to use an alternate config file is a bit funky. There are a couple of ways to do this. The approach I use is as follows:

  1. Create a soft-link to the top executable. This does not have to be done as root, as long as you have write access to the link's location...

    ln -s /usr/bin/top /home/myusername/bin/omgwtf
    
  2. Launch top by typing the name of the link ('omgwtf') rather than 'top'. You will be in normal top interactive mode, but when you save the configuration file it will write to ~/.omgwtfrc, leaving ~/.toprc alone.

  3. Disable the summary area and write the configuration file same as before (press 'l', 'm', 't' and 'W')

In the future, when you're ready to run top without summary info in batch mode, you'll have to invoke top via the link name you created. For example,

% omgwtf -usyslog -bn1
PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
576 syslog    20   0  264496   8144   1352 S   0.0  0.1   0:03.66 rsyslogd
%
Paperclip Bob
  • 346
  • 2
  • 8
9

If you're running top in batch mode (-b -n1), just delete the header lines with sed:

top -b -n1 | sed 1,7d

That will remove the first 7 header lines that top outputs and returns only the processes.

David Parks
  • 30,789
  • 47
  • 185
  • 328
  • Make it `top -cbn 1 |awk '{ if (NR<7 || $9=="0.0") { next }; print $0}'` so you will only get cpu hungry processes filtered by $9=="0.0" where $9 is cpu usage row. – Orsiris de Jong Jun 03 '23 at 09:23
5

It's known as the "Summary Area" and i don't think there is a way at top initialization to disable those.

But while top is running, you can disable those by pressing l, t, m.

From man top:

 Summary-Area-defaults
  'l' - Load Avg/Uptime  On  (thus program name)
  't' - Task/Cpu states  On  (1+1 lines, see '1')
  'm' - Mem/Swap usage   On  (2 lines worth)
  '1' - Single Cpu       On  (thus 1 line if smp)
heemayl
  • 39,294
  • 7
  • 70
  • 76
  • 2
    But I aimed for running it in batch mode. Anyways thanks for re-iterating my conclusion on this. – sjsam May 03 '16 at 04:17
1

This will dump the output and it can be redirected to any file if needed.

top -n1  |grep -Ev "Tasks:|Cpu(s):|Swap:|Mem:"
monk
  • 1,953
  • 3
  • 21
  • 41
1

To monitoring a particular process, following command is working for me -

top -sbn1 -p $(pidof <process_name>) | grep $(pidof <process_name>)

And to get the all process information you can use the following -

top -sbn1|sed -n '/PID/,/^$/p'
Anupam Bera
  • 519
  • 5
  • 9
0

egrep may be good enough in this case, but I would add that perl -lane could do this kind of thing with lightning speed:

top -b -n 1 | perl -lane '/PID/ and $x=1; $x and print' | head -n10

This way you may forget the precise arguments for grep, sed, awk, etc. for good because perl is typically much faster than those tools.

mnish
  • 385
  • 2
  • 11
0

On a mac you cannot use -b which is used in many of the other answers.

In that case the command would be top -n1 -l1 | sed 1,10d

Grabbing only the first process line (and its header), only logging once, instead of interactive, then suppress the general information for top command which are the first 10 lines.

jasonleonhard
  • 12,047
  • 89
  • 66