rss logo

Script Bash qui redémarre un système si le ping vers un hôte échoue

Voici un script Bourne Shell qui redémarre un ordinateur automatiquement lorsque le ping vers un hôte échoue.

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 #--------------------------------------------------- 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
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :

contact mail address