4
2
I'm trying to write a small bash script that checks for internet connection. If no internet is detected, it tries again in another 10 minutes... and if still no internet, it restarts the computer. I've written this thus far. Any ideas to suggest improvement / efficiency / better code is appreciated.
#!/bin/bash
# Script to restart the computer if there is no internet connection
# within the next 10 minutes of whenever the script is run, which
# I'll setup via crontab to run every half an hour.
IS=`/bin/ping -c 5 8.8.8.8 | grep -c "64 bytes"`
if (test "$IS" -gt "2") then
internet_conn="1"
exit
else
internet_conn="0"
sleep 600
AA=`/bin/ping -c 5 74.125.226.18 | grep -c "64 bytes"`
if (test "$AA" -gt "2") then
internet_conn="1"
exit
else
sudo shutdown -r now
fi
fi
3Just curious: What's the point of rebooting when your link goes down ? What do you think will happen when google takes down/recycle that IP and it doesn't return ICMP anymore ? If you want to do that anyway you'd better choose that IP: 8.8.8.8 (google public dns server) – Shadok – 2011-12-09T16:28:23.633
1This computer uses a script to initiate a reverse ssh tunnel. Sometimes, due to network problems, the sockets go stale and active connections through tunnel hang, so a reboot would start everything fresh. As per your suggestion, I've changed the ip. Good catch. – Sparctus – 2011-12-09T16:32:29.290
I don't really see a question with a real answer, maybe this should be moved to the Code Review stack exchange site?
– Caspar – 2011-12-09T16:37:58.830Ohh, didn't realize such a site existed. Probably should be moved then. – Sparctus – 2011-12-09T16:39:48.837