Skip to content

Instantly share code, notes, and snippets.

@Dottenpixel
Last active August 29, 2015 13:58
Show Gist options
  • Save Dottenpixel/10434496 to your computer and use it in GitHub Desktop.
Save Dottenpixel/10434496 to your computer and use it in GitHub Desktop.
Reboot server if connection lost (credit to http://ubuntuforums.org/archive/index.php/t-1889951.html)
#!/bin/bash
HOSTS="router"
COUNT=4
for myHost in $HOSTS
do
count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
echo "Ping count of $count to $myHost at $(date)" >> ~/log/monitor.log
if [[ $count == '' ]]; then
echo "Host : connection is down (ping failed) at $(date)" >> ~/log/monitor.log
reboot
fi
if [ $count -ne 4 ]; then
# partial fail
echo "Host : $myHost is only partially connectable (ping failed) at $(date)" >> ~/log/monitor.log
reboot
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment