【问题标题】:The command top -b -n 1| grep Cpu always returns the same value命令 top -b -n 1| grep Cpu 总是返回相同的值
【发布时间】:2015-09-23 11:07:44
【问题描述】:

当我运行命令top -b -n 1| grep Cpu 时,它总是返回相同的值:

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

但是当我删除-n 1 部分时,结果以

开头
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 

然后后面的值就不一样了。

有什么理由吗?如何获得不同的值?

【问题讨论】:

  • 您查看过-n 1 选项的作用吗?如果你这样做了,答案将是显而易见的。

标签: linux shell grep


【解决方案1】:

检查您的 top 手册页:

   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.

【讨论】:

  • 但是这个信息是在错误部分而不是主要部分中提到的。谢谢。
【解决方案2】:

您定义选项 -n max 将让 top 执行 max 迭代。如果你给了1,你只会得到1次迭代的数据。当然还有单行数据。

我不知道你会如何使用输出。 top -b -n x|grep Cpu 可能会给出不同的输出,具体取决于 top 的版本。在我的 Archlinux 上,top -b -n 1 |grep Cpu 的输出是:

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[||||||||||||||||||||||||||||||||||||||||||||||||||||||         ]

【讨论】:

    猜你喜欢
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 2021-02-13
    • 2019-10-22
    • 2019-10-31
    相关资源
    最近更新 更多