Skip to content

Instantly share code, notes, and snippets.

@mharizanov
Created April 6, 2013 08:50
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save mharizanov/5325450 to your computer and use it in GitHub Desktop.
Save mharizanov/5325450 to your computer and use it in GitHub Desktop.
Script to check and re-connect WiFi on Raspberry Pi
#!/bin/bash
##################################################################
# A Project of TNET Services, Inc
#
# Title: WiFi_Check
# Author: Kevin Reed (Dweeber)
# dweeber.dweebs@gmail.com
# Project: Raspberry Pi Stuff
#
# Copyright: Copyright (c) 2012 Kevin Reed <kreed@tnet.com>
# https://github.com/dweeber/WiFi_Check
#
# 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
#
# Instructions:
#
# o Install where you want to run it from like /usr/local/bin
# o chmod 0755 /usr/local/bin/WiFi_Check
# 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/WiFi_Check
#
##################################################################
# Settings
# Where and what you want to call the Lockfile
lockfile='/var/run/WiFi_Check.pid'
# Which Interface do you want to check/fix
wlan='wlan0'
pingip='192.168.1.1'
##################################################################
echo
echo "Starting WiFi check for $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"
/bin/ping -c 2 -I $wlan $pingip > /dev/null 2> /dev/null
if [ $? -ge 1 ] ; then
echo "Network connection down! Attempting reconnection."
/sbin/ifdown $wlan
/bin/sleep 5
/sbin/ifup --force $wlan
else
echo "Network is Okay"
fi
echo
echo "Current Setting:"
/sbin/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
##################################################################
@guidobender
Copy link

Fantastic. Just what I needed. It works perfect for me. Thanks for putting this out there.
Much appreciated.

@torsj
Copy link

torsj commented Dec 7, 2017

Exactly what I needed.

I just changed the interface restart function because my RPi use dhcpcd so ifup / ifdown doesn't work:

    /sbin/dhcpcd -n $wlan
    #/sbin/ifdown $wlan
    #/bin/sleep 5
    #/sbin/ifup --force $wlan

Thanks!

@sonatique
Copy link

Thanks a lot for sharing this!

@lex3001
Copy link

lex3001 commented Dec 11, 2019

I also changed the code a bit because for whatever reason ifup and ifdown don't work on my Pi. In my logs I found these errors:

Dec 10 06:30:13 raspberrypi WiFi_Check: ifdown: unknown interface wlan0
Dec 10 06:30:18 raspberrypi WiFi_Check: ifup: unknown interface wlan0

After doing some research I found out that ifconfig will work on my Pi even through ifup and ifdown don't.

echo "Network connection down! Attempting reconnection."
echo "Taking interface down..."
ifconfig $wlan down
# /sbin/ifdown $wlan
sleep 5
echo "Bringing interface up..."
ifconfig $wlan up
# /sbin/ifup --force $wlan

This worked in a test so hopefully will solve my woes!

BTW this is what I did my crontab file so that I would be able to log the output:
*/5 * * * * root /home/pi/Documents/WiFi_Check 2>&1 | logger -t WiFi_Check

@waynewarburton
Copy link

Fantastic. Just what I needed. It works perfect for me. Thanks for putting this out there.
Much appreciated.

Newbie here. I'm up and running but lost my WIFI a couple times now. My power is from the generator's battery so when it gets serviced, I lose the WIFI. I'd like to include this check in my program but have no idea how or where to place it. Can anyone give the basic steps to achieve this?

@lex3001
Copy link

lex3001 commented Mar 23, 2021

Newbie here. I'm up and running but lost my WIFI a couple times now. My power is from the generator's battery so when it gets serviced, I lose the WIFI. I'd like to include this check in my program but have no idea how or where to place it. Can anyone give the basic steps to achieve this?

Are you saying that when your power goes out to your Pi, and it turns off when it powers back up it is not connecting to WiFi? I don't think that is what this script will help with. but you could try it. How are you accessing your Pi after it powers up?

@krahul9473
Copy link

lockfile='/var/run/WiFi_Check.pid'

what is the meaning of line. I am using mac system so i m unable to go var directory and this code i will run in raspberry pi or in my local system.

please help me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment