top command first iteration always returns the same result

后端 未结 5 923
故里飘歌
故里飘歌 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:38

    Here's an example to computes total CPU usage for ALL processes in top. I use -d flag to increase the default interval in order to smooth out the value. Not using tail because the header can be specific to Linux distribution.

    top -b -d 5 -n 2 | awk '$1 == "PID" {block_num++; next} block_num == 2 {sum += $9;} END {print sum}'
    

提交回复
热议问题