Before you start, make sure ip command is available on your system. In modern Linux distributions, ip replaces older ifconfig command. If net-tools package (that includes ifconfig) is not installed and you prefer using it, you can install it via sudo apt-get install net-tools.
Use touch /home/pi/wifi-reconnect.sh to create a shell script file,
with the following content:
#!/bin/bash
SSID=$(/sbin/iwgetid --raw)
if [ -z "$SSID" ]; then
echo "`date -Is` WiFi interface is down, trying to reconnect" >> /home/pi/wifi-log.txt
if command -v /sbin/ip &> /dev/null; then
/sbin/ip link set wlan0 down
sleep 10
/sbin/ip link set wlan0 up
elif command -v sudo ifconfig &> /dev/null; then
sudo ifconfig wlan0 down
sleep 10
sudo ifconfig wlan0 up
else
echo "`date -Is` Failed to reconnect: neither /sbin/ip nor ifconfig commands are available" >> /home/pi/wifi-log.txt
fi
fi
echo 'WiFi check finished'Or you can also use the following command to download the script.
sudo wget https://raw.github.com/carry0987/Raspberry-Pi-Repo/master/Auto-WiFi-Reconnect/wifi-reconnect.shThis script uses /sbin/ip command for disabling and enabling the wlan interface. This is a more modern approach compared to using ifconfig and is the preferred way in newer Linux distributions.
sudo chmod +x /home/pi/wifi-reconnect.sh
sudo apt-get install cron
Use sudo vim /etc/crontab to edit crontab
By putting the following content at the end of the file:
* * * * * root /home/pi/wifi-reconnect.sh
Test it by disconnecting from WiFi:
/sbin/ip link set wlan0 down
The script should reestablish the connection within 1 minute.
After the RPi reestablishes the connection, reconnect to the RPi and check the log file:
cat /home/pi/wifi-log.txt

I have a Powerwall 2 and rpi5 running Trixie and was getting disconnections to the PW WiFi.
Tried out the script on Trixie, but it it also using network manager and wants
nmcli connection down/up <SSID>Which works well for me.
I’m not super familiar with Network Manager, so not sure the best way to add that in as a check and option for your script.
Can maybe test by using
nmcli connection show | grep $SSIDto see if configured.I also did some one time configs:
Set the auto retries for that SSID, as suggested by @cvaziri1
sudo nmcli connection modify TEG-XXX connection.autoconnect-retries 0and set the band to only b/g/n
sudo nmcli connection modify TEG-XXX 802-11-wireless.band bgand disabled power saving
sudo nmcli connection modify TEG-XXX 802-11-wireless.powersave 2