Last active
July 4, 2016 13:11
-
-
Save cafuego/fcc6eb2f5fbfa7bad3618dc216267502 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Wait for the system load to hit a set level. Can be used in init | |
# scripts to avoid insane load spikes. Use the return value to abort | |
# starting processes if the load remains too high. | |
# | |
# wait_timeout.sh 4 || exit 0 | |
# Default sleep interval in seconds. | |
SLEEP=30 | |
# How many sleeps before we give up? | |
TIMEOUT=10 | |
# Unless we get a CLI param, wait until the system load is 2(.something) or lower. | |
typeset -i TARGET=${1:-2} | |
if [ $TARGET -eq 0 ]; then | |
typeset -i TARGET=2 | |
fi | |
echo "Waiting until the system load reaches $TARGET. Will give up after $((SLEEP * $TIMEOUT)) seconds." | |
typeset -i COUNT=0 | |
typeset -i LOAD=$(cut -d. -f1 < /proc/loadavg) | |
while [ $LOAD -gt $TARGET ]; do | |
if [ $COUNT -gt $TIMEOUT ]; then | |
echo "Reached the timeout of $((SLEEP * $TIMEOUT)) seconds, giving up." | |
exit 1 | |
fi | |
echo "Current load $LOAD is above $TARGET, sleeping for $SLEEP seconds..." | |
sleep $SLEEP | |
typeset -i LOAD=$(cut -d. -f1 < /proc/loadavg) | |
COUNT=$(($COUNT+1)) | |
done | |
echo "Current load $LOAD is below $TARGET, continuing." | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment