Skip to content

Instantly share code, notes, and snippets.

@tblong
Created May 29, 2019 23:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tblong/eec5ae1353f9ad9277845efc1760b4bb to your computer and use it in GitHub Desktop.
Save tblong/eec5ae1353f9ad9277845efc1760b4bb to your computer and use it in GitHub Desktop.
Checks a pingable source and reboots the host when the failure limit is reached.
#!/bin/bash
#
# Original Source Attribution:
# Location: https://gist.github.com/SandroMachado/87e591fc42f368636b251b566485ae46
# Author: Sandro Machado
# Date of retrieval: 2019-05-28
#
# Script Summary:
# This script checks a pingable source and when the failure
# limit is reached the host is restarted. No files are written
# to with this script.
#
# This script is intended to be ran via cron with the @reboot
# (see man 5 crontab) option so that it runs once at startup and
# continues execution in an infinite loop until a reboot
# condition is reached.
#
# Modifications the original source are made under the following license:
#
# MIT License
#
# Copyright (c) 2019 Tyler B. Long
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
####################
# Configurations
####################
# The interval [sec] to check the pingable destination for connectivity.
check_interval=120
# The public or local resource to ping.
ping_destination=10.189.200.1
# The number of ping packets to send.
ping_count=3
# The consecutive max ping failure count. When reached a reboot is invoked.
max_ping_failures=5
####################
# Main
####################
failure_count=0
while true
do
# test the pingable resource
ping -c $ping_count $ping_destination > /dev/null 2>&1
# check exit status of ping command
if [[ $? -eq 0 ]]; then
failure_count=0
else
failure_count=$((failure_count+1))
if ((failure_count >= max_ping_failures)); then
sudo /sbin/shutdown -r +1
break
fi
fi
sleep $check_interval
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment