加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-13 19:33:46 所属栏目:Linux 来源:网络整理
导读:在bash脚本中,我需要等到CPU使用率低于阈值. 换句话说,我需要一个命令wait_until_cpu_low,我将这样使用: # Trigger some background CPU-heavy commandwait_until_cpu_low 40# Some other commands executed when CPU usage is below 40% 我该怎么办? 编辑
在bash脚本中,我需要等到CPU使用率低于阈值.

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

# Trigger some background CPU-heavy command
wait_until_cpu_low 40
# Some other commands executed when CPU usage is below 40%

我该怎么办?

编辑:

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

解决方法

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

注意它需要安装sysstat包.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读