在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包.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。