#!/bin/sh ################################################################## # # Title: check_wlan0 # # Script checks to see if WiFi has a network IP and if not # restart WiFi # # Uses a lock file which prevents the script from running more # than one at a time. If lockfile is old, it removes it # # Instructions: # # o Install where you want to run it from like /usr/local/bin # o chmod 0755 /usr/local/bin/check_wlan0.sh # o Add to crontab # # Run Every 5 mins - Seems like ever min is over kill unless # this is a very common problem. If once a min change */5 to * # once every 2 mins */5 to */2 ... # # */5 * * * * /usr/local/bin/check_wlan0.sh # ################################################################## # Settings # Where and what you want to call the Lockfile lockfile='/var/run/check_wlan0.pid' #logger "logfile: $lockfile" # Which Interface do you want to check/fix # Example: wlan0, eth0, etc. wlan='wlan0' ################################################################## echo echo "check: $wlan" #date echo # Check to see if there is a lock file if [ -e $lockfile ]; then # A lockfile exists... Lets check to see if it is still valid pid=`cat $lockfile` if kill -0 &>1 > /dev/null $pid; then # Still Valid... lets let it be... echo "Process still running, Lockfile valid" exit 1 else # Old Lockfile, Remove it echo "Old lockfile, Removing Lockfile" rm $lockfile fi fi # If we get here, set a lock file using our current PID# #echo "Setting Lockfile" echo $$ > $lockfile # We can perform check echo "Performing Network check for $wlan" #if ifconfig $wlan | grep -q "inet addr:" ; then if /sbin/ifconfig $wlan | /bin/grep "inet addr:" ; then echo "Network is Okay" #logger "Network is Okay" else echo "Network connection down! Attempting reconnection." /sbin/ifdown $wlan logger "Network connection down! Attempting reconnection." logger "ifdown $wlan - sleep 5" /bin/sleep 5 /sbin/ifup --force $wlan /sbin/ifconfig $wlan | grep "inet addr" logger `/sbin/ifconfig $wlan | grep "inet addr"` /bin/sleep 5 fi echo echo "Current Setting:" ifconfig $wlan | grep "inet addr:" echo # Check is complete, Remove Lock file and exit echo "process is complete, removing lockfile" rm $lockfile exit 0 ################################################################## # End of Script ##################################################################