Skip to content

Commit 15d5783

Browse files
committed
Add reboot loop protection
1 parent b93da9b commit 15d5783

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
# Raspberry Pi / Linux Network Repair Automation
1+
# Network Repair/Reboot Automation
22
> This bash script checks the health status for either wired or wireless internet connection and, if it is failing, tries to fix it.
33
4-
## Prerequisite - Mandatory
4+
## Tested Platforms
5+
Raspberry Pi Linux
6+
ReadyNAS 6.10.3
7+
8+
## Prerequisites
59

610
You at least need the "ping" command which should exist on most systems which already have networking. If not the following should get you there:
711

@@ -15,7 +19,7 @@ You at least need the "ping" command which should exist on most systems which al
1519
`sudo crontab -e`
1620
- Add to your crontab the following line, it will execute the check every minute. Please customize the script path according to the folder where you cloned the repo:
1721
`* * * * * /yourpath/network_check.sh >> /var/log/netcheck.log 2>&1`
18-
- If you also want to reboot in case wifi is not working after the fix customize the reboot_server variable accordigly editing the script:
22+
- If you also want to reboot in case the network is not working after the fix customize the reboot_server variable accordigly editing the script:
1923
`nano network_check.sh`
2024

2125
## Optional - Push notifications / Email

network_check.sh

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@
1010

1111
# Set gateway_ip to the gateway that you want to check to declare network working or not
1212
gateway_ip='1.1.1.1'
13-
# Set nic to your Network card name, as seen in ifconfig output
13+
# Set nic to your Network card name, as seen in ip output
1414
nic='wlan0'
1515
# Set network_check_threshold to the maximum number of failed checks
1616
network_check_threshold=5
1717
# Set reboot_server to true if you want to reboot as a last
18-
# option to fix wifi if ifconfig up/down
18+
# option to fix wifi if ip up/down fail
1919
reboot_server=false
20+
# to prevent reboot loops, only reboot once every N minutes
21+
reboot_cycle=60
22+
# last boot file
23+
last_bootfile=/root/.last_net_autoboot
2024

2125
###
2226
# Script logic
@@ -30,7 +34,7 @@ function date_log {
3034
}
3135

3236
function restart_wlan {
33-
# Trying wlan restart using ifconfig
37+
# Trying wlan restart using ip
3438
date_log "Network was not working for the previous $network_check_tries checks."
3539
date_log "Restarting $nic"
3640
/sbin/ip link set "$nic" down
@@ -41,9 +45,15 @@ function restart_wlan {
4145
# If network is still down and reboot_server is set to true reboot
4246
ping -c 1 $gateway_ip > /dev/null 2>&1
4347
if [[ $? != 0 ]]; then
44-
if [ "$reboot_server" = true ] ; then
45-
date_log "Network is still not working, rebooting"
46-
/sbin/reboot
48+
if [ "$reboot_server" = true ]; then
49+
# if there's no last boot file or it's older than reboot_cycle
50+
if [[ ! -f $last_bootfile || $(find $last_bootfile -mtime +$reboot_cycle -print) ]]; then
51+
touch $last_bootfile
52+
date_log "Network is still not working, rebooting"
53+
/sbin/reboot
54+
else
55+
date_log "Last auto reboot was less than $reboot_cycle minutes old"
56+
fi
4757
fi
4858
fi
4959
}

0 commit comments

Comments
 (0)