WiFi Reconnection Script

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.

Normally what I would so 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.

Basically it uses the NetworkManager CLI utility 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.

bash
#!/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

This runs every minute from a cron job in /etc/cron.d/wifi-reconnect:

cron
* * * * * root /path/to/script/wifi-reconnect.sh >> /var/log/wifi-reconnect.log 2>&1

The log file looks like this:

plaintext
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