How to suppress the general information for top command
Asked Answered
S

7

12

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.

Scleroprotein answered 3/5, 2016 at 4:0 Comment(3)
@heemayl : Updated the question.Scleroprotein
top -b | egrep -v '^(top|Tasks:|Cpu|Mem:|Swap:)' perhaps? Or are looking for a zero-piping solution?Vanadous
@MichaelAlbers : Thankyou but I am looking for a zero piping solutionScleroprotein
F
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)
Franconia answered 3/5, 2016 at 4:15 Comment(1)
But I aimed for running it in batch mode. Anyways thanks for re-iterating my conclusion on this.Scleroprotein
C
11

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
%
Corunna answered 27/5, 2017 at 0:58 Comment(0)
D
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.

Delinda answered 30/9, 2019 at 23:8 Comment(1)
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.Seline
F
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)
Franconia answered 3/5, 2016 at 4:15 Comment(1)
But I aimed for running it in batch mode. Anyways thanks for re-iterating my conclusion on this.Scleroprotein
T
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:"
Thilda answered 3/5, 2016 at 7:33 Comment(2)
Thanks for the time anyways. Appreciate it.Scleroprotein
What if there is some process running, containing one of these words? It will be silently ignored. Not good...Insuppressible
E
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'
Erichericha answered 11/8, 2018 at 20:51 Comment(0)
H
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.

Hendecasyllable answered 22/5, 2018 at 10:43 Comment(1)
I enjoyed your elitist disrespect of the Unix way coupled with your head dependency.Afrikah
N
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.

Norwich answered 10/8, 2020 at 18:49 Comment(2)
Sorted by something useful is nice too, consider adding in -o cpuNorwich
Obviously you would only sort if you removed the -n1Norwich

© 2022 - 2024 — McMap. All rights reserved.