1

This question already has an answer here:

after connecting to a network via wpa_supplicant I need a way to know if we are disconnected so we can connect again automatically.

marked as duplicate by lenik, goldilocks, RPiAwesomeness, Fred, Impulss Apr 9 '14 at 5:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

I've copied and modified a script for this (the copyright-claims are the originals) :

I did some modifications, the one I can remember is adding the silence option. place this script in /home/pi/.wifi_check, add execution rights (chmod a+x /home/pi/.wifi_check) and add this line to /etc/crontab/ to let it test the connection every 5 minutes:
*/5 * * * * root /home/pi/.wifi_check silent

#!/bin/bash
##################################################################
# A Project of TNET Services, Inc
# Original verion can be found at:
# https://github.com/dweeber/WiFi_Check
# This version is modified by:
# ExploWare  http://raspberrypi.stackexchange.com/users/13296/exploware
#
# Purpose:
#
# 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
#
#
##################################################################
##################################################################
# Settings
# Where and what you want to call the Lockfile
lockfile='/run/lock/WiFi_Check.pid'
# Which Interface do you want to check/fix
wlan='wlan0'
wired='eth0'
silent=false
if [[ "$1" == "silent" ]]; then
    silent=true
fi
##################################################################
#echo
$silent || echo -n "Starting WiFi check for $wlan"
#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"
        $silent || echo -n .
        exit 1
    else
        # Old Lockfile, Remove it
        #echo "Old lockfile, Removing Lockfile"
        $silent || echo -n .
        rm $lockfile
        sleep .5
    fi
else
    $silent || echo -n .
fi
# If we get here, set a lock file using our current PID#
#echo "Setting Lockfile"
$silent || echo -n .
echo $$ > $lockfile

# We can perform check
#echo "Performing Network check for $wlan"
if iwconfig $wlan|grep -qe "ESSID:\".*\""; then
    #echo ESSID found
    $silent || echo -n .
    if ifconfig $wlan | grep -q "inet addr:" ; then
        #echo "Network is Okay"
        $silent || echo . OK
        logger Wifi connection on $wlan is OK
        ifdown $wired 2&>1 > /dev/null 
    elif ifconfig $wlan | grep -q "inet6 addr:" ; then
        #echo "Network \(IPv6 only\) is Okay"
        $silent || echo . OK
        logger Wifi connection on $wlan \(IPv6 only\) is OK
        ifdown $wired 2&>1 > /dev/null 
    else
        $silent || echo "Network connection down! Attempting reconnection."
        logger Wifi connection on $wlan is DOWN, trying to reconnect... 
        date
        ifdown $wlan
        sleep 5
        ifup --force $wlan
        if ifconfig $wlan | grep -q "inet addr:" ; then
            logger Wifi connection on $wlan seems to be up and running again.
            ifdown $wired 2&>1 > /dev/null 
        else
            logger Wifi connection on $wlan is DOWN, enabling the wired connection...
            ifup $wired 2&>1 > /dev/null 
        fi
    fi
else
    $silent || echo "Network connection down! Attempting reconnection."
    logger Wifi connection on $wlan is DOWN, trying to reconnect... 
    date
    ifdown $wlan
    sleep 5
    ifup --force $wlan
    if ifconfig $wlan | grep -q "inet addr:" ; then
        logger Wifi connection on $wlan seems to be up and running again.
        ifdown $wired 2&>1 > /dev/null 
    else
        logger Wifi connection on $wlan is DOWN, enabling the wired connection...
        ifup $wired 2&>1 > /dev/null 
    fi
fi

$silent || echo -n "Current Setting:"
$silent || iwconfig $wlan | grep -oe "ESSID:\".*\"";ifconfig $wlan | grep "inet addr:"

# Check is complete, Remove Lock file and exit
#echo "process is complete, removing lockfile"
rm $lockfile

##################################################################
# End of Script
##################################################################
  • This is a solution to your problem, but the exact answer to your question is: see if a network address is assigned to your interface (ifconfig $wlan | grep -q "inet addr:") – ExploWare Mar 18 '14 at 21:32
  • what about this solution ls /sys/class/net/wlan0/operstate – user12448 Mar 18 '14 at 21:40
  • @user12448 My interface can be up, searching for the accesspoint, while not being connected. There could be a flaw in this script, it could be only working with DHCP, havent tested in static-configuration – ExploWare Mar 18 '14 at 21:43
  • your solution is good but how to make it work in ubuntu there is no file called wifi_check pid (i start always testing – user12448 Mar 18 '14 at 21:46
  • the pid file is only in its place when this script is running. It contains the ProcessID of this script. That way it can make sure there is only one instance running, so you cannot accidentally start it multiple times – ExploWare Mar 18 '14 at 21:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.