top命令第一次迭代总是返回相同的结果

时间:2011-02-09 00:42:02

标签: linux batch-file admin centos5 top-command

运行top -b -n 1时,该命令始终返回相同的CPU值。 连续5次考虑以下测试运行:

[user@server ~]$ top -b -n 5 -d.2 | grep "Cpu(s)"
Cpu(s): 18.5%us, 10.0%sy,  0.0%ni, 67.0%id,  4.2%wa,  0.0%hi,  0.2%si,  0.1%st
Cpu(s): 39.8%us, 27.7%sy,  0.0%ni, 31.3%id,  0.0%wa,  0.0%hi,  1.2%si,  0.0%st
Cpu(s): 39.0%us, 35.4%sy,  0.0%ni, 23.2%id,  0.0%wa,  0.0%hi,  1.2%si,  1.2%st
Cpu(s): 41.2%us, 34.1%sy,  0.0%ni, 15.3%id,  1.2%wa,  0.0%hi,  2.4%si,  5.9%st
Cpu(s): 59.0%us, 30.1%sy,  0.0%ni,  4.8%id,  0.0%wa,  0.0%hi,  3.6%si,  2.4%st
[user@server ~]$ top -b -n 5 -d.2 | grep "Cpu(s)"
Cpu(s): 18.5%us, 10.0%sy,  0.0%ni, 67.0%id,  4.2%wa,  0.0%hi,  0.2%si,  0.1%st
Cpu(s): 18.9%us, 17.8%sy,  0.0%ni, 63.3%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu(s): 18.8%us, 21.2%sy,  0.0%ni, 55.3%id,  2.4%wa,  0.0%hi,  1.2%si,  1.2%st
Cpu(s): 29.4%us, 24.7%sy,  0.0%ni, 45.9%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu(s): 60.5%us, 24.4%sy,  0.0%ni, 11.6%id,  1.2%wa,  0.0%hi,  1.2%si,  1.2%st
[user@server ~]$ top -b -n 5 -d.2 | grep "Cpu(s)"
Cpu(s): 18.5%us, 10.0%sy,  0.0%ni, 67.0%id,  4.2%wa,  0.0%hi,  0.2%si,  0.1%st
Cpu(s): 43.4%us, 38.6%sy,  0.0%ni, 15.7%id,  0.0%wa,  0.0%hi,  1.2%si,  1.2%st
Cpu(s): 55.3%us, 40.0%sy,  0.0%ni,  4.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu(s): 39.5%us, 48.8%sy,  0.0%ni,  5.8%id,  1.2%wa,  0.0%hi,  1.2%si,  3.5%st
Cpu(s): 40.7%us, 55.6%sy,  0.0%ni,  2.5%id,  0.0%wa,  0.0%hi,  0.0%si,  1.2%st
[user@server ~]$ top -b -n 5 -d.2 | grep "Cpu(s)"
Cpu(s): 18.5%us, 10.0%sy,  0.0%ni, 67.0%id,  4.2%wa,  0.0%hi,  0.2%si,  0.1%st
Cpu(s): 27.1%us, 10.6%sy,  0.0%ni, 61.2%id,  1.2%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu(s): 25.3%us,  5.7%sy,  0.0%ni, 67.8%id,  0.0%wa,  0.0%hi,  0.0%si,  1.1%st
Cpu(s): 15.5%us, 16.7%sy,  0.0%ni, 64.3%id,  0.0%wa,  0.0%hi,  0.0%si,  3.6%st
Cpu(s): 57.3%us, 11.2%sy,  0.0%ni, 30.3%id,  0.0%wa,  0.0%hi,  0.0%si,  1.1%st
[user@server ~]$ top -b -n 5 -d.2 | grep "Cpu(s)"
Cpu(s): 18.5%us, 10.0%sy,  0.0%ni, 67.0%id,  4.2%wa,  0.0%hi,  0.2%si,  0.1%st
Cpu(s): 44.0%us,  6.0%sy,  0.0%ni, 42.9%id,  0.0%wa,  0.0%hi,  3.6%si,  3.6%st
Cpu(s): 45.8%us,  9.6%sy,  0.0%ni, 44.6%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu(s): 34.5%us,  8.3%sy,  0.0%ni, 57.1%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu(s): 38.6%us, 14.5%sy,  0.0%ni, 45.8%id,  0.0%wa,  0.0%hi,  0.0%si,  1.2%st

知道这里可能出现什么问题?

5 个答案:

答案 0 :(得分:8)

如果有人正在寻找一行获取cpu的值,那么试试这个

top -b -n 5 -d.2 | grep "Cpu" | tail -n1 | awk '{print($2)}' | cut -d'%' -f 1

答案 1 :(得分:5)

CPU利用率数据计算为一段时间内的平均值。对于第一次迭代,该时间间隔是“从系统启动到现在”;对于后续迭代,时间间隔是“从最后一次迭代到现在”。

答案 2 :(得分:4)

Top在启动之前不知道处理时间计数是多少,因此它会根据负载平均值和等待线程等各种因素在第一遍中进行猜测。

如果以交互方式启动顶部并快速检查它返回的第一批结果,您可以看到相同的效果。

简单解决方案:top -b -n 5 -d.2 | grep "Cpu(s)" | tail -n+2

答案 3 :(得分:2)

这是一个计算top中所有进程的总CPU使用率的示例。我使用-d标志来增加默认间隔以平滑值。不使用tail,因为标题可以特定于Linux发行版。

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

答案 4 :(得分:1)

我也遇到了这个问题并做了一些挖掘。

如果你'人顶'并且向下滚动,你将从第7节中找到以下内容。 。

top命令通过查看样本之间CPU时间值的变化来计算Cpu(s)。当您第一次运行它时,它没有先前要比较的样本,因此这些初始值是自引导以来的百分比。这意味着您至少需要两个循环,或者您必须忽略第一个循环的摘要输出。这是例如批处理模式的问题。如果定义CPULOOP = 1环境变量,则可能有解决方法。 top命令将在标准输出之前为CPU数据运行一个额外的隐藏循环。

希望这有帮助!