linux – Bash:等到CPU使用率低于阈值

前端之家收集整理的这篇文章主要介绍了linux – Bash:等到CPU使用率低于阈值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在bash脚本中,我需要等到cpu使用率低于阈值.

换句话说,我需要一个命令wait_until_cpu_low,我将这样使用:

  1. # Trigger some background cpu-heavy command
  2. wait_until_cpu_low 40
  3. # Some other commands executed when cpu usage is below 40%

我该怎么办?

编辑:

目标操作系统是:Red Hat Enterprise Linux Server 6.5版
>我正在考虑平均cpu使用率(跨所有内核)

解决方法

  1. wait_for_cpu_usage()
  2. {
  3. current=$(mpstat 1 1 | awk '$12 ~ /[0-9.]+/ { print int(100 - $12 + 0.5) }')
  4. while [[ "$current" -ge "$1" ]]; do
  5. current=$(mpstat 1 1 | awk '$12 ~ /[0-9.]+/ { print int(100 - $12 + 0.5) }')
  6. sleep 1
  7. done
  8. }

注意它需要安装sysstat包.

猜你在找的Linux相关文章