I am logging on to a node which I know has near 100% cpu usage (20 single process jobs using nearly 100% cpu each). When I use top interactively, the first iteration, it gives me about 20% ni and the next iteration and all after it correctly gives 95+% ni usage. I am wanting to pipe the output of top via top -bn1 > outfile, but the first iteration of top -bn1 (the only iteration) gives the incorrect CPU usage. If I use top -bn2, then the second iteration gives the correct usage, but that is too much output.

How can I get top -bn1 to correctly give me the CPU usage? I am writing this script for usage statistics, so if necessary, I can go back and run an analysis on the processes independently to generate my own CPU usage, but it would be nice if top would give me the correct usage right off the bat.

EDIT: mpdstat -P ALL gives me the same, incorrect, initial usage statistics. It would be nice to get that figured out as well too. I can use mpstat -P ALL 1 1, but this gives the output twice.

  • ni refers to the CPU usage influenced by process "niceness." Just issuing the top command with default priority will interfere with that number. (I'm assuming here that these "20 single process jobs using nearly 100% cpu [sic] each" have a non-zero nice value.)
    – Joe Sewell
    Mar 18, 2015 at 16:56
  • It sounds like you are saying that I am causing the 100% ni value by invoking top. However, when I top on an idle node, I get 0.0%ni as expected.
    – drjrm3
    Mar 19, 2015 at 17:19
  • Sorry, I'm trying to say just the opposite. The 20% ni value is due to invoking top, which is using more "not nice" CPU (which would be reported in the us category, more than likely). The first invocation causes the drop because of invoking top. That's why the second value appears more correct.
    – Joe Sewell
    Mar 19, 2015 at 17:21
  • So you are saying that the ni value is dropped AT FIRST due to me invoking top and then it goes back to normal on the second iteration? Why would it change at all since top -bn2 is just invoking two iterations of top?
    – drjrm3
    Mar 19, 2015 at 19:03
  • The second "iteration" doesn't include starting up top itself.
    – Joe Sewell
    Mar 23, 2015 at 15:19

2 Answers 2


Just drop the output of the first iteration.

top -bn2 | awk '/^top -/ { p=!p } { if (!p) print }'
  • So what's wrong with -bn1? I have the feeling -bn1 shows longer time average, as I found it changes slower. Not sure if this is the case?
    – Qian Chen
    Nov 27, 2015 at 10:58
  • 2
    The first iteration includes the start up of top itself and is therefore inaccurate.
    – FloHimself
    Nov 27, 2015 at 11:38
  • 1
    @ElgsQianChen: see this comment.
    – Campa
    Apr 16, 2018 at 8:12

There are tools which first display an calculated average. Other tools have an incomplete first sample. Overall it is advised to skip the first sample to get the desired result.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .