Last active
January 13, 2024 21:17
-
-
Save mjdargen/1816c601919d6403f919c90cafebc03d to your computer and use it in GitHub Desktop.
Shell script to periodically reconnect wifi if raspberry pi disconnects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ping router every 5 minutes, reconnect if fails | |
*/5 * * * * /usr/bin/sudo /home/pi/Documents/reconnect_wlan0.sh > /dev/null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
gateway='192.168.0.1' | |
# ping router to check connection | |
ping -c4 ${gateway} > /dev/null | |
# '$?' is the exit code of previous ping command. | |
# if exit code != 0 (failure) | |
if [ $? != 0 ] | |
then | |
echo "$(date): No network connection, restarting wlan0" >> /var/log/checkwifi.log | |
# turn off wifi | |
/sbin/ip link set wlan0 down | |
sleep 5 | |
# restart wifi | |
/sbin/ip link set wlan0 up | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment