In a bash script I need to wait until CPU usage gets below a threshold.
在bash脚本中,我需要等到CPU使用率低于阈值。
In other words, I'd need a command wait_until_cpu_low
which I would use like this:
换句话说,我需要一个命令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%
How could I do that?
我怎么能这样做?
Edit:
编辑:
- target OS is: Red Hat Enterprise Linux Server release 6.5
- 目标操作系统是:Red Hat Enterprise Linux Server 6.5版
- I'm considering the average CPU usage (across all cores)
- 我正在考虑平均CPU使用率(跨所有核心)
3 个解决方案
#1
3
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
}
Notice it requires sysstat package installed.
请注意,它需要安装sysstat软件包。
#2
3
You might use a function based on the top
utility. But note, that doing so is not very reliable because the CPU utilization might - rapidly - change at any time. Meaning that just because the check succeeded, it is not guaranteed that the CPU utilization will stay low as long the following code runs. You have been warned.
您可以使用基于*实用程序的函数。但请注意,这样做并不十分可靠,因为CPU利用率可能会随时快速变化。这意味着仅仅因为检查成功,就不能保证只要以下代码运行,CPU利用率就会保持低水平。你被警告了。
The function:
功能:
function wait_for_cpu_usage {
threshold=$1
while true ; do
# Get the current CPU usage
usage=$(top -n1 | awk 'NR==3{print $2}' | tr ',' '.')
# Compared the current usage against the threshold
result=$(bc -l <<< "$usage <= $threshold")
[ $result == "1" ] && break
# Feel free to sleep less than a second. (with GNU sleep)
sleep 1
done
return 0
}
# Example call
wait_for_cpu_usage 25
Note that I'm using bc -l
for the comparison since top prints the CPU utilization as a float value.
请注意,我使用bc -l进行比较,因为top将CPU利用率打印为浮点值。
#3
3
A much more efficient version just calls mpstat
and awk
once each, and keeps them both running until done; no need to explicitly sleep
and restart both processes every second (which, on an embedded platform, could add up to measurable overhead):
一个效率更高的版本只需要调用mpstat和awk一次,并保持它们一直运行直到完成;无需每秒显式地休眠和重启两个进程(在嵌入式平台上,这可能会增加可衡量的开销):
wait_until_cpu_low() {
awk -v target="$1" '
$13 ~ /^[0-9.]+$/ {
current = 100 - $13
if(current <= target) { exit(0); }
}' < <(mpstat 1)
}
I'm using $13
here because that's where idle %
is for my version of mpstat; substitute appropriately if yours differs.
我在这里使用13美元,因为这是我的mpstat版本的空闲%;如果你的不同,请适当替换。
This has the extra advantage of doing floating point math correctly, rather than needing to round to integers for shell-native math.
这具有正确执行浮点数学的额外优势,而不是需要舍入到整数以进行shell本机数学运算。
#1
3
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
}
Notice it requires sysstat package installed.
请注意,它需要安装sysstat软件包。
#2
3
You might use a function based on the top
utility. But note, that doing so is not very reliable because the CPU utilization might - rapidly - change at any time. Meaning that just because the check succeeded, it is not guaranteed that the CPU utilization will stay low as long the following code runs. You have been warned.
您可以使用基于*实用程序的函数。但请注意,这样做并不十分可靠,因为CPU利用率可能会随时快速变化。这意味着仅仅因为检查成功,就不能保证只要以下代码运行,CPU利用率就会保持低水平。你被警告了。
The function:
功能:
function wait_for_cpu_usage {
threshold=$1
while true ; do
# Get the current CPU usage
usage=$(top -n1 | awk 'NR==3{print $2}' | tr ',' '.')
# Compared the current usage against the threshold
result=$(bc -l <<< "$usage <= $threshold")
[ $result == "1" ] && break
# Feel free to sleep less than a second. (with GNU sleep)
sleep 1
done
return 0
}
# Example call
wait_for_cpu_usage 25
Note that I'm using bc -l
for the comparison since top prints the CPU utilization as a float value.
请注意,我使用bc -l进行比较,因为top将CPU利用率打印为浮点值。
#3
3
A much more efficient version just calls mpstat
and awk
once each, and keeps them both running until done; no need to explicitly sleep
and restart both processes every second (which, on an embedded platform, could add up to measurable overhead):
一个效率更高的版本只需要调用mpstat和awk一次,并保持它们一直运行直到完成;无需每秒显式地休眠和重启两个进程(在嵌入式平台上,这可能会增加可衡量的开销):
wait_until_cpu_low() {
awk -v target="$1" '
$13 ~ /^[0-9.]+$/ {
current = 100 - $13
if(current <= target) { exit(0); }
}' < <(mpstat 1)
}
I'm using $13
here because that's where idle %
is for my version of mpstat; substitute appropriately if yours differs.
我在这里使用13美元,因为这是我的mpstat版本的空闲%;如果你的不同,请适当替换。
This has the extra advantage of doing floating point math correctly, rather than needing to round to integers for shell-native math.
这具有正确执行浮点数学的额外优势,而不是需要舍入到整数以进行shell本机数学运算。