Created
November 13, 2017 21:37
-
-
Save caioaao/4011046590e0e2d81da99e863ff66dd3 to your computer and use it in GitHub Desktop.
Bash script for connection self-healing (to use on my raspberry pi/beaglebone)
This file contains hidden or 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
#!/usr/bin/env bash | |
set -euo pipefail | |
HEARTBEAT_INTERVAL=3 | |
EXPONENTIAL_BACKOFF_POWER=2 | |
EXPONENTIAL_BACKOFF_MAX_TIMEOUT=60 | |
EXPONENTIAL_BACKOFF_INITIAL_TIMEOUT=1 | |
NETWORK_PROFILE=wlp2s0-nu-office | |
while true; do | |
if ( ping -q -c 1 google.com ); then | |
echo "pong" | |
sleep $HEARTBEAT_INTERVAL | |
else | |
echo "oh noes" | |
timeout=$EXPONENTIAL_BACKOFF_INITIAL_TIMEOUT | |
while true; do | |
if ( netctl switch-to $NETWORK_PROFILE); then | |
echo "connection restored" | |
break | |
else | |
timeout=$(( timeout * EXPONENTIAL_BACKOFF_POWER )) | |
if ( $timeout > $EXPONENTIAL_BACKOFF_MAX_TIMEOUT ); then | |
timeout=$EXPONENTIAL_BACKOFF_MAX_TIMEOUT | |
fi | |
echo "connection failed. sleeping for $timeout seconds" | |
sleep $timeout | |
fi | |
done | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment