WiFi Reconnection Script[source]
xml
<glacius:metadata> | |
<title>WiFi Reconnection Script</title> | |
<description>Little script to automatically reconnect to wifi, e.g. on a Raspberry Pi</description> | |
<category>Code snippets</category> | |
<category>Programming</category> | |
<category>Bash</category> | |
</glacius:metadata> | |
<p> | |
I recently upgraded my magic mirror from a 1st gen Raspberry Pi B to a Raspberry Pi 4. | |
The RPi4 has a builtin WiFi adapter so I don't need the dongle. I thought it would be | |
more stable, but so far not so much. It can't connect to my 2.4GHz SSID but kinda | |
connects to the 5GHz SSID. However, it periodically refuses to reconnect. And since | |
the Pi is locked inside a frame, duct-taped to the back of a monitor, it's not easy | |
to rip it off the wall to troubleshoot. | |
</p> | |
<p> | |
Normally what I would do is just unplug and plug it back in to make it reconnect, but | |
that only seemed like a reasonable thing to do on a Pi from 2012, but not one from this | |
decade. So I wrote this cron script that kind of works. | |
</p> | |
<p> | |
Basically it uses the | |
<a href="https://www.networkmanager.dev/docs/api/latest/nmcli.html">NetworkManager CLI utility</a> | |
to see if the WiFi is connected, | |
and if not, it cycles the radio off and then on. In the event that this doesn't work | |
for 15 minutes (900 seconds), it just reboots the whole machine. I'm tired of trying | |
to debug this nonsense so I'm taking a scorched Earth approach now. | |
</p> | |
<glacius:code lang="bash"><![CDATA[ | |
#!/bin/bash | |
tempFile=/tmp/wifi-reconnect | |
if [[ ! -f "${tempFile}" ]]; then | |
date +%s > "${tempFile}" | |
fi | |
if ! nmcli connection show --active | grep wifi > /dev/null; then | |
lastConnected=$(cat "${tempFile}") | |
threshold=900 | |
if [[ -n "${lastConnected}" && $(( lastConnected + threshold )) -lt $(date +%s) ]]; then | |
echo "$(date) not connected, haven't connected for ${threshold}s (last: ${lastConnected}), rebooting..." | |
/usr/sbin/shutdown -r now | |
exit | |
fi | |
echo "$(date) not connected, cycling wifi radio..." | |
nmcli radio wifi off | |
nmcli radio wifi on | |
else | |
echo "$(date) connected" | |
date +%s > "${tempFile}" | |
fi | |
]]></glacius:code> | |
<p> | |
This runs every minute from a cron job in <code>/etc/cron.d/wifi-reconnect</code>: | |
</p> | |
<glacius:code lang="cron"><![CDATA[* * * * * root /path/to/script/wifi-reconnect.sh >> /var/log/wifi-reconnect.log 2>&1 | |
]]></glacius:code> | |
<p> | |
The log file looks like this: | |
</p> | |
<glacius:code lang="plaintext"><![CDATA[Sun 14 Apr 09:29:01 PDT 2024 connected | |
Sun 14 Apr 09:30:01 PDT 2024 connected | |
Sun 14 Apr 09:31:01 PDT 2024 connected | |
Sun 14 Apr 09:32:01 PDT 2024 connected | |
Sun 14 Apr 09:33:01 PDT 2024 not connected, cycling wifi radio... | |
Sun 14 Apr 09:34:01 PDT 2024 connected | |
Sun 14 Apr 09:35:01 PDT 2024 not connected, cycling wifi radio... | |
Sun 14 Apr 09:36:01 PDT 2024 connected | |
Sun 14 Apr 09:37:01 PDT 2024 not connected, cycling wifi radio... | |
Sun 14 Apr 09:38:01 PDT 2024 not connected, cycling wifi radio... | |
Sun 14 Apr 09:39:01 PDT 2024 not connected, cycling wifi radio... | |
Sun 14 Apr 09:40:01 PDT 2024 not connected, cycling wifi radio... | |
Sun 14 Apr 09:41:02 PDT 2024 not connected, cycling wifi radio... | |
Sun 14 Apr 09:42:01 PDT 2024 not connected, cycling wifi radio... | |
Sun 14 Apr 09:43:01 PDT 2024 not connected, cycling wifi radio... | |
Sun 14 Apr 09:44:01 PDT 2024 not connected, cycling wifi radio... | |
Sun 14 Apr 09:45:01 PDT 2024 not connected, cycling wifi radio... | |
Sun 14 Apr 09:46:01 PDT 2024 not connected, cycling wifi radio... | |
Sun 14 Apr 09:47:02 PDT 2024 not connected, cycling wifi radio... | |
Sun 14 Apr 09:48:01 PDT 2024 not connected, cycling wifi radio... | |
Sun 14 Apr 09:49:01 PDT 2024 not connected, cycling wifi radio... | |
Sun 14 Apr 09:50:01 PDT 2024 not connected, cycling wifi radio... | |
Sun 14 Apr 09:51:01 PDT 2024 not connected, cycling wifi radio... | |
Sun 14 Apr 09:52:01 PDT 2024 not connected, haven't connected for 900s (last: 1713112561), rebooting... | |
Sun 14 Apr 09:53:27 PDT 2024 connected | |
Sun 14 Apr 09:54:01 PDT 2024 connected | |
Sun 14 Apr 09:55:01 PDT 2024 connected | |
Sun 14 Apr 09:56:01 PDT 2024 connected | |
]]></glacius:code> | |