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: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest 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: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “Beginners”