> For the complete documentation index, see [llms.txt](https://carry0987.gitbook.io/raspberry-pi-note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://carry0987.gitbook.io/raspberry-pi-note/book_source/article/06.rpi3-auto-reconnect-wifi.md).

# RPi3 Auto reconnect to wifi when lost connect

## Create script file

Use `touch /home/pi/wifi-reconnect.sh` to create shell script file, with following content:

```
#!/bin/bash 
 
SSID=$(/sbin/iwgetid --raw) 

if [ -z "$SSID" ] 
then 
    echo "`date -Is` WiFi interface is down, trying to reconnect" >> /home/pi/wifi-log.txt
    sudo ifconfig wlan0 down
    sleep 30
    sudo ifconfig wlan0 up 
fi 

echo "WiFi check finished"
```

Or you can also use `sudo wget https://raw.github.com/carry0987/Raspberry-Pi-Repo/master/Auto-WiFi-Reconnect/wifi-reconnect.sh` to download the script

## Make new file executable

`sudo chmod +x /home/pi/wifi-reconnect.sh`

## Install cron

`sudo apt-get install cron`

## Edit crontab

Use `sudo vim /etc/crontab` to edit **crontab**

By putting following content at end of file:

`* * * * * root /home/pi/wifi-reconnect.sh`

Test it by disconnecting from WiFi:

`sudo ifconfig wlan0 down`

Script should reestablish connection within 1 minute.

## Check log file

After the RPi reestablish connection, reconnect RPi and check log file:\
`cat /home/pi/wifi-log.txt` ![RPi3-Auto-Reconnect-WiFi](https://1629731818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTkx9vj8rDQgjbGkVku%2F-LTlDC2gRIuHSiDNZUjc%2F-LTlDD2S_vvAgD3YI511%2Fimg_1.jpg?generation=1544866556219163\&alt=media)
