Raspberry PI 4 is dropping wifi

I have some Raspberry pi 4 and the wifi connection is always dropping. So that after searching on the web I found some solutions to this issue. The first one is to set the correct locale, which could be US:

iw reg set US

or

iw reg set EU

The seond solution is that one that consists of creating a cron task that re-enables wifi when it drops. So that I modified the original script in order to make it work with Ubuntu Server 23.10:

#!/bin/bash
wlan='wlan0'
pingip='google.com'

/bin/ping -c 2 $pingip > /dev/null 2> /dev/null
if [ $? -ge 1 ] ; then
    sudo /bin/ip link set wlan0 down
    sleep 5
    sudo /bin/ip link set wlan0 up
    echo "re-connected"
else
        echo "already connected"
fi

Save the content above in wlan-up.sh

Assign execution attributes:

sudo chmod +x wlan-up.sh

Add a cron task:

sudo crontab -e

Add the following configuration and replace the YOUR_PATH with your’s .sh file path:

*/1 * * * * /YOUR_PATH/wlan-up.sh > /dev/null 2>&1

Reload the new configuration:

sudo service cron reload

Or restart you Raspberry Pi 4

sudo reboot

Now if you disable the wifi with the bellow statement, after 1 minute it will be reenabled by cron job:

sudo ip link set wlan0 down