PDA

View Full Version : Linux bash/shell script pause if load average higher/greater than



Fli
11-16-2014, 06:42 PM
When running an shell script one may want to pause it if load average is higher/greater than some value..

Easy command to get load average on linux:


cut -d . -f 1 /proc/loadavg

... it returns whole load average numbers (3, 10, 150)

so if i want to pause script on high load average (sleep 5 seconds) if loadavg is greater than 10. i add this condition to my shell script:


if [ "$(cut -d . -f 1 /proc/loadavg)" -gt "10" ];then
sleep 5
fi

--------

but what if we do not want to continue running script at all if load is too high? We use WHILE loop instead of IF


while [ "$(cut -d . -f 1 /proc/loadavg)" -gt "10" ];do
sleep 5
done

-------

but what if load average will be high too long time and we want to wait only limitted time? We enhance WHILE loop by loop counter and if counter match some number we do some action like break the loop


loopnumber=0
while [ "$(cut -d . -f 1 /proc/loadavg)" -gt "10" ];do
loopnumber=$((loopnumber+1))
echo "Sleeping until load goes down. This is load check number $loopnumber out of 100"
sleep 5
if [ "$loopnumber" -gt "99" ];then
break
fi
done