user120612
user120612

Reputation: 49

The command top -b -n 1| grep Cpu always returns the same value

When I run the command top -b -n 1| grep Cpu, it always returns the same values:

Cpu(s): 0.3%us, 0.3%sy, 0.0%ni, 99.4%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st

but when I remove the -n 1 part, the results start with

Cpu(s): 0.3%us, 0.3%sy, 0.0%ni, 99.4%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st 

and then the subsequent values are different.

Any reason for this? How can I get different values?

Upvotes: 2

Views: 986

Answers (2)

Kent
Kent

Reputation: 195049

you define option -n max will let top do max iterations. If you gave 1, you will only get the data from 1 iteration. And get of course the single line data.

I don't know if how would you use the output. top -b -n x|grep Cpu could give different output depends on the version of top E.g. on my Archlinux, the output of top -b -n 1 |grep Cpu is:

kent$ (master|…) top -b -n 1 |grep Cpu
%Cpu0  :  12.7/4.1    17[|||||||||||                                                    ]
%Cpu1  :  69.9/17.3   87[|||||||||||||||||||||||||||||||||||||||||||||||||||||||        ]
%Cpu2  :  69.0/19.2   88[|||||||||||||||||||||||||||||||||||||||||||||||||||||||        ]
%Cpu3  :  68.1/18.0   86[||||||||||||||||||||||||||||||||||||||||||||||||||||||         ]

Upvotes: 1

Fazlin
Fazlin

Reputation: 2337

Check your top man page:

   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.

Upvotes: 2

Related Questions