Intro

Here is a Bourne Shell script which can automatically reboot a computer if he cannot ping a host.

Code

#! /bin/bash
# autoreboot.sh version 1.0
# 2017.01.15  shebangthedolphins.net first version
#---------------------------------------------------
# this script automatically reboot a computer if he cannot ping a host
#---------------------------------------------------

#! /bin/bash

i=0
LOG="/var/log/auto_reboot.log"

IP="10.10.1.22"
IP2="10.10.1.20"
ping -c 1 -q "$IP" >/dev/null 2>&1 || ping -c 1 -q "$IP2" >/dev/null 2>&1
RES="$?"

if [ "$RES" != "0" ]; then
        while [ "$i" -lt 5 ]
        do
                sleep 10s
                ping -c 1 -q "$IP" >/dev/null 2>&1 || ping -c 1 -q "$IP2" >/dev/null 2>&1
                RES="$?"
                if [ "$RES" != "0" ]; then
                        i=$(( $i + 1 ))
                        echo "$(LC_ALL=en_EN.utf8 date "+%b %e %H:%M:%S") LOG : error n°$i" >> "$LOG"
                        if [ "$i" == "5" ]; then
                                echo "$(LC_ALL=en_EN.utf8 date "+%b %e %H:%M:%S") LOG : reboot" >> "$LOG"
                                reboot
                        fi
                else
                        echo "$(LC_ALL=en_EN.utf8 date "+%b %e %H:%M:%S") LOG : warning but no reboot" >> "$LOG"
                        exit 0
                fi
        done
else
        echo "$(LC_ALL=en_EN.utf8 date "+%b %e %H:%M:%S") LOG : OK" >> "$LOG"
fi


Licence Creative Commons
This websitehttp://shebangthedolphins.net is licensed to the public under a licence Creative Commons Attribution licence.
Contact :