I made a cron that did a ping test on a DNS server to ensure networking. Something like this:
ping 8.8.8.8 -c 1 -i .2 -t 60 > /dev/null 2>&1
ONLINE=$?
if [ ONLINE -eq 0 ]; then
#We're offline
else
#We're online
fi
Recently I've used something like this:
#!/bin/bash
function check_online
{
netcat -z -w 5 8.8.8.8 53 && echo 1 || echo 0
}
# Initial check to see if we are online
IS_ONLINE=check_online
# How many times we should check if we're online - this prevents infinite looping
MAX_CHECKS=5
# Initial starting value for checks
CHECKS=0
# Loop while we're not online.
while [ $IS_ONLINE -eq 0 ]; do
# We're offline. Sleep for a bit, then check again
sleep 10;
IS_ONLINE=check_online
CHECKS=$[ $CHECKS + 1 ]
if [ $CHECKS -gt $MAX_CHECKS ]; then
break
fi
done
if [ $IS_ONLINE -eq 0 ]; then
# We never were able to get online. Kill script.
exit 1
fi
# Now we enter our normal code here. The above was just for online checking
This isn't the MOST elegant - I'm not sure how else to check via a simple command or file on the system, but this has worked for me when needed.