Skip to content

Instantly share code, notes, and snippets.

@mjdargen
Last active January 13, 2024 21:17
Show Gist options
  • Save mjdargen/1816c601919d6403f919c90cafebc03d to your computer and use it in GitHub Desktop.
Save mjdargen/1816c601919d6403f919c90cafebc03d to your computer and use it in GitHub Desktop.
Shell script to periodically reconnect wifi if raspberry pi disconnects.
# ping router every 5 minutes, reconnect if fails
*/5 * * * * /usr/bin/sudo /home/pi/Documents/reconnect_wlan0.sh > /dev/null
#!/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