The script below restarts wifi on your raspberry pi when wifi crashes or your pi cant connect to your network.
Login via ssh to your raspberry pi,
default username is: pi
default password is: raspberry
Go to folder /usr/local/bin
create a file restartwifi.sh
Copy and paste the contents below into the file,
change 192.168.0.1 to ip address of your router.
press ctrl+x to exit nano, save the file with y.
now we have to create a cronjob which will check wifi every 15 minutes, and if there is no connection to router to restart it.
select nano as editor.
add at the end of file this line:
press ctrl+x to exit nano, save the file with y.
Login via ssh to your raspberry pi,
default username is: pi
default password is: raspberry
Go to folder /usr/local/bin
Code:
cd /usr/local/bin
Code:
sudo nano restartwifi.sh
change 192.168.0.1 to ip address of your router.
Code:
#!/bin/bash # The IP for the server you wish to ping (8.8.8.8 is a public Google DNS server) SERVER=192.168.0.1 # Only send two pings, sending output to /dev/null ping -c2 ${SERVER} > /dev/null # If the return code from ping ($?) is not 0 (meaning there was an error) if [ $? != 0 ] then # Restart the wireless interface #ifdown --force wlan0 #ifup wlan0 ifconfig wlan0 down sleep 5 ifconfig wlan0 up fi
now we have to create a cronjob which will check wifi every 15 minutes, and if there is no connection to router to restart it.
Code:
sudo nano crontab -e
add at the end of file this line:
Code:
*/15 * * * * /usr/bin/sudo -H /usr/local/bin/restartwifi.sh >> /dev/null 2>&1
Comment