Created
March 13, 2023 12:01
-
-
Save v3rm0n/2089ffacd5ffc03b14fd1825d8311765 to your computer and use it in GitHub Desktop.
Reboot server on connectivity loss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
[ "$UID" -eq 0 ] || exec sudo bash "$0" "$@" | |
((count = 10)) # Maximum number to try. | |
autoreboot_log="/var/log/autoreboot.log" | |
echo $(date -u) "Current time: $(date)" >> "$autoreboot_log" | |
while [[ $count -ne 0 ]] ; do | |
/usr/bin/ping -c 1 1.1.1.1 >> "$autoreboot_log" 2>&1 | |
rc=$? | |
if [[ $rc -eq 0 ]] ; then | |
echo $(date -u) "Connectivity present, exiting" >> "$autoreboot_log" | |
((count = 1)) # If okay, flag loop exit. | |
else | |
echo $(date -u) "Retrying $count" >> "$autoreboot_log" | |
sleep 10 # Minimise network storm. | |
fi | |
((count = count - 1)) # So we don't go forever. | |
done | |
if [[ $rc -eq 1 ]] ; then | |
echo $(date -u) "No connectivity, rebooting" >> "$autoreboot_log" | |
echo $(date -u) "-------------------------------------------" >> "$autoreboot_log" | |
/usr/bin/systemctl start reboot.target | |
else | |
echo $(date -u) "-------------------------------------------" >> "$autoreboot_log" | |
fi | |
exit $rc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment