Skip to content

Instantly share code, notes, and snippets.

@caioaao
Created November 13, 2017 21:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caioaao/4011046590e0e2d81da99e863ff66dd3 to your computer and use it in GitHub Desktop.
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)
#!/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