Bash: wait until CPU usage gets below a threshold Bash: wait until CPU usage gets below a threshold linux linux

Bash: wait until CPU usage gets below a threshold


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):

wait_until_cpu_low() {    awk -v target="$1" '    $13 ~ /^[0-9.]+$/ {      current = 100 - $13      if(current <= target) { exit(0); }    }' < <(LC_ALL=C mpstat 1)}

I'm using $13 here because that's where idle % is for my version of mpstat; substitute appropriately if yours differs.

This has the extra advantage of doing floating point math correctly, rather than needing to round to integers for shell-native math.


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.


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.

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 callwait_for_cpu_usage 25

Note that I'm using bc -l for the comparison since top prints the CPU utilization as a float value.