Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress the general information for top command

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.

like image 926
sjsam Avatar asked May 03 '16 04:05

sjsam


3 Answers

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
%
like image 115
Paperclip Bob Avatar answered Sep 22 '22 02:09

Paperclip Bob


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.

like image 28
David Parks Avatar answered Sep 20 '22 02:09

David Parks


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)
like image 22
heemayl Avatar answered Sep 21 '22 02:09

heemayl