Skip to content

Instantly share code, notes, and snippets.

@hlapp
Created June 10, 2018 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hlapp/cee3bfb735caca2eea2c6e317ea932e4 to your computer and use it in GitHub Desktop.
Save hlapp/cee3bfb735caca2eea2c6e317ea932e4 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Reboots if there is no network device for the global route.
#
# Hilmar Lapp <hlapp@drycafe.net>
#
# To the extent possible under law, I have waived all copyright and related
# or neighboring rights to this work under the CC0 1.0 Universal Public Domai
# Dedication. See https://creativecommons.org/publicdomain/zero/1.0
# log message level
#
# Log to standard output if in a terminal, and to syslog otherwise. Level
# defaults to info.
function log() {
level=$2
if [ -z "$level" -a ! -t 1 ] ; then
level=info
fi
if [ -t 1 ] ; then
if [ -n "$level" ] ; then
level="${level}: "
fi
echo "$level$1"
else
logger -p local0.$level -t "watchdog" "$1"
fi
}
# determine the network interface that was supposed to be connected
dev=$(ip route list scope global | cut -d" " -f5)
# if there's no device then reboot unless we're already rebooting
if [ -z "$dev" ] ; then
if [ -e /tmp/reboot ] ; then
log "No network device for global route. Rebooting already in progress?" error
exit 0
fi
touch /tmp/reboot
log "No network device for global route. Rebooting now." error
(sleep 3 && /sbin/reboot) &
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment