Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Wifi reconnect

Sun Oct 04, 2015 2:42 pm

Hello, my raspberry pi can works a few days without me, i buy offical wi-fi usb module then choose my network and wrote password - the internet is working, but raspberry can lose network and if i want connect to wifi again i must restart raspberry or remove and insert wifi module, how to make it automatically (if raspberry lose network it must to try to reconnect)?

User avatar
DougieLawson
Posts: 34568
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Wifi reconnect

Sun Oct 04, 2015 3:04 pm

Try running this every ten minutes from crontab.

Code: Select all

#!/bin/bash

router=`ip route | awk '/default/ {print $3}'`
/bin/ping -q -c1 $router > /dev/null

if [ $? -eq  0 ]
then
  : #  echo "Network active"

else
  echo "Network down, fixing..."
#  ifdown --force wlan0
  /bin/kill -9 `pidof wpa_supplicant`
  /sbin/ifup --force wlan0
  /sbin/ip route add default via $router dev wlan0
  /bin/mount -a
fi
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a hippy & doctor free zone.

Return to “Beginners”