CPU usage in top batch mode
I have a problem that makes me mad. I am running top in batch mode with the following command,
top -b -n 1
T开发者_如何学Pythonhe problem is I can run top in batch mode 100 times but the CPU usage never changes past the original value. The memory usage changes as expected but CPU stays the same. If I simultaneously run another top in a different window the CPU usage is changing for that top but not for the top in batch mode.
Basically CPU stats don't seem to change in batch mode and do in interactive mode. Does anyone know why? Try it yourself, run the above command a few times and observe the CPU usage staying the same then run top in interactive mode and observe the CPU usage constantly changing.
On the first iteration, it is showing you the average CPU usage since system startup.
(Note that this is no longer the case for newer versions of top
).
Here's one liner that displays cumulative CPU usage over a longer period of time, 5 seconds in this case. You can adjust it with -d flag.
top -b -d 5 -n 2 | awk '$1 == "PID" {block_num++; next} block_num == 2 {sum += $9;} END {print sum}'
精彩评论