Skip to content

Instantly share code, notes, and snippets.

@jethrocarr
Created July 22, 2017 09:28
Show Gist options
  • Save jethrocarr/a5dae9fe8523cf74d30a065d77d74876 to your computer and use it in GitHub Desktop.
Save jethrocarr/a5dae9fe8523cf74d30a065d77d74876 to your computer and use it in GitHub Desktop.
#!/bin/bash -x
#
# Local startup customisations. Everything logs to syslog
#
(
exec 1> >(logger -s -t rc-local) 2>&1
# Remove the eth0 default route
/sbin/route del default eth0
# Start up our HTTP server for health checks. We run this in a bash shell
# to ensure automatic restarts if it crashes
(
while true
do
echo "(re)starting the HTTP health check server"
sudo -u nobody /usr/local/bin/pinghttpserver
sleep 1
done
) &
# Setup a wvdial connection with automatic reconnection
# in the background. Only issue is if the PPP process
# terminates itself and leaves the 3G modem in an unclean state,
# we are unable to power cycle it.
(
i="0"
while [ $i -lt 5 ]
do
# Trigger an update of our dynamic DNS in 60 seconds time
(
sleep 60
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
# Put your dynamic DNS update hook here. I personally use:
# https://github.com/awslabs/route53-dynamic-dns-with-lambda
) &
# Reconnect wvdial
echo "(re)starting wvdial for ${i}/10"
/usr/bin/wvdial
# wvdial has terminated!
sleep 30
i=$[$i+1]
done
# We've failed... too many restarts of wvdial
echo "Too many restarts, power cycling entire device"
# Power Cycle USB. We do this to fix a bug where the firmware on the 3G modem crashes.
# Note: This kills *all* USB power, except for the ethernet controller, not just the 3G modem.
#
# Note2: This is *very* hardware specific and works on a Raspberry Pi 2. Other models or devices may/will vary.
/usr/local/bin/hub-ctrl -h 0 -P 2 -p 0
sleep 5
/usr/local/bin/hub-ctrl -h 0 -P 2 -p 1
echo "Rebooting..."
reboot
) &
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment