Skip to content

Instantly share code, notes, and snippets.

@Nebucatnetzer
Last active September 21, 2022 06:28
Show Gist options
  • Save Nebucatnetzer/827643fb3170d445c62f241f32568831 to your computer and use it in GitHub Desktop.
Save Nebucatnetzer/827643fb3170d445c62f241f32568831 to your computer and use it in GitHub Desktop.
A simple script to reboot my IPFire box if the internet isn't reachable for 45 minutes. Goes to /etc/fcron.cyclic/
#!/usr/bin/env bash
reboot_index=$(($(</var/run/reboot_index)))
reset-index(){
echo 0 > /var/run/reboot_index
echo "$(date +'%Y-%m-%dT%H:%M:%S%z'): $1" >> /var/log/reboot_if_offline.log
}
if [ "$(find /var/run/reboot_index -mmin +1)" != "" ] && [ $reboot_index != 0 ]; then
reset-index "index to old, reset counter"
exit 0
fi
ping -c1 -W1 -q 8.8.8.8 &>/dev/null
status=$( echo $? )
if [ $status == 0 ] && [ $reboot_index -gt 0 ]; then
reset-index "we're online: reset counter"
exit 0
fi
if [ $status != 0 ]; then
let reboot_index++
echo $reboot_index > /var/run/reboot_index
echo "$(date +'%Y-%m-%dT%H:%M:%S%z'): we're offline, increase index $reboot_index" >> /var/log/reboot_if_offline.log
fi
if [ $reboot_index -ge 8 ]; then
reset-index "we're still offline, try reboot"
/sbin/reboot
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment