Skip to content

Instantly share code, notes, and snippets.

@fengye
Created September 29, 2019 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fengye/4b28c10d1bd1283c77dcbcf47d6eaf85 to your computer and use it in GitHub Desktop.
Save fengye/4b28c10d1bd1283c77dcbcf47d6eaf85 to your computer and use it in GitHub Desktop.
Raspberry Pi constantly drop wifi and won't be able to reconnect itself. Run this script on a cron job on a regular basis to keep headless pi connected.
#!/bin/bash
#=================================================================
# Script Variables Settings
wlan='wlx801f02b56d73'
gateway='192.168.0.1'
alias ifup='/sbin/ifup'
alias ifdown='/sbin/ifdown'
alias ifconfig='/sbin/ifconfig'
#=================================================================
date
echo " - Auto Reconnect Wi-Fi Status for $wlan Script Started ";
# Only send two pings, sending output to /dev/null as we don't want to fill logs on our sd card.
# If you want to force ping from your wlan0 you can connect next line and uncomment second line
ping -c2 ${gateway} > /dev/null # ping to gateway from Wi-Fi or from Ethernet
# ping -I ${wlan} -c2 ${gateway} > /dev/null # only ping through Wi-Fi
# 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
sleep 5
ifup wlan0
fi
ping -I ${wlan} -c2 ${gateway} > /dev/null
date
echo " - Auto Reconnect Wi-Fi Status for $wlan Script Ended ";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment