top command first iteration always returns the same result

后端 未结 5 893
故里飘歌
故里飘歌 2021-01-12 16:12

When running top -b -n 1, the command always returns the same CPU values. Consider the following test run 5 times in succession:

[user@server ~]$ top -b -n          


        
5条回答
  •  独厮守ぢ
    2021-01-12 16:45

    I ran into this issue as well and did some digging.

    If you 'man top' and scroll waaaay down, you'll find the following from section 7 . . .

    The top command calculates Cpu(s) by looking at the change in CPU time values between samples. When you first run it, it has no previous sample to compare to, so these initial values are the percentages since boot. It means you need at least two loops or you have to ignore summary output from the first loop. This is problem for example for batch mode. There is a possible workaround if you define the CPULOOP=1 environment variable. The top command will be run one extra hidden loop for CPU data before standard output.

    Hope this helps!

提交回复
热议问题