Automatically Restart Dropped WiFi Connections
Here is a revised service for those who have issues with their WiFi routinely disconnecting. For those who have tried everything to fix their connection drops without success this service may help.
It is always preferable to fix your connectivity issues by changing driver options, firmware, kernel version, etc, etc. Unfortunately some adapters still have disconnection issues regardless of the fixes you apply. This is a massive annoyance if your adapter disconnects regularly. This service will hopefully alleviate that inconvenience somewhat by automatically restarting your network connection for you.
Some users are unlucky enough to have to reboot every time their WiFi goes down. This service should restart your networking components without requiring a reboot. Even if you don't need to reboot, you will find this service far more convenient than manually restarting your connection.
This service pings Google's servers to determine if your connection is up. If your connection is down, then the service will automatically initiate a restart of all network components. This should revive your connection without any need for manual intervention. Sometimes a recurring ping as used in this service also helps to keep a shaky connection alive.
This service should automatically restart your network connection shortly after it drops. From the time when it detects a loss of connectivity the script can be as quick as 5 seconds in re-initiating a connection.
To get this network auto restart functionality you must create one service file and one associated script.
Network Restart Service:
With a text editor create the systemd service file:
/etc/systemd/system/network-restart.service
With the following contents:
# to terminate running service & script: sudo systemctl stop network-restart.service && sudo killall -9 network_restart
#/etc/systemd/system/network-restart.service
#sudo systemctl enable network-restart.service
#sudo systemctl start network-restart.service
#sudo systemctl stop network-restart.service
#sudo systemctl disable network-restart.service
#systemctl status network-restart.service
#sudo systemctl daemon-reload
[Unit]
Description=Network Restart Service
WantedBy=network.target
[Service]
User=root
Type=simple
Restart=always
RestartSec=3
RemainAfterExit=yes
Restart=on-failure
StartLimitIntervalSec=0
ExecStartPre=-sleep 15
ExecStart=/usr/local/sbin/network_restart.sh
[Install]
WantedBy=network.target
Once you have created and saved the service file, enable the service:
sudo systemctl enable network-restart.service
Network Restart Script:
With a text editor create:
/usr/local/sbin/network_restart.sh
network_restart.sh script contents:
#!/bin/bash
# to terminate running service & script: sudo systemctl stop network-restart.service && sudo killall -9 network_restart
#/usr/local/sbin/network_restart.sh
while true; do
ping -c 1 8.8.8.8 | grep received
if [ $? -eq 0 ]; then sleep 2
else
echo "Connection broken, restarting network connection"
/bin/sh -c 'nmcli networking off'
systemctl stop NetworkManager
ip link set wlp3s0 down
modprobe -r rtl8723be
sleep 1
modprobe rtl8723be
sleep .5
ip link set wlp3s0 up
sleep .5
systemctl start NetworkManager
/bin/sh -c 'nmcli networking on'
/bin/sh -c 'nmcli r wifi off'
sleep .5
/bin/sh -c 'nmcli r wifi on'
sleep 10
break
fi
done
exec "$ScriptLoc"/usr/local/sbin/network_restart.sh && exit
Make the script executable:
sudo chmod +x /usr/local/sbin/network_restart.sh
The group of numbers "8.8.8.8" is Googles server address. If you would prefer to ping another server such as Cloudflare then change the address to "1.1.1.1" instead.
There are several lines in the script that will need to be modified if you have different networking components. You can find the required information by running the command "inxi -n". You must replace "wlp3s0" with your adapters designation returned from the "inxi -n" command if your identification is different.
ip link set wlp3s0 down
ip link set wlp3s0 up
You must also insert your driver module in place of "rtl8723be" in the script if your adapter uses a different module. Modify these lines (as below) in the script to reflect the driver module your adapter uses:
modprobe -r rtl8723be
modprobe rtl8723be
Drivers such as the Atheros ath10k or Intel iwlwifi drivers use several modules rather than only one. If your adapter uses several drivers the script will need to be modified to list both of the modules it uses.
After creating the service file and the associated script reboot the computer.
After rebooting you can use the following command to test how well the connection is reestablished after it is broken:
nmcli networking off
This service and script are designed to be self-respawning. This makes the service and script available to continuously refresh your internet connection. It also makes it very difficult to interrupt the script temporarily. To terminate the running service and script you must issue this command:
sudo systemctl stop network-restart.service && sudo killall -9 network_restart
I hope this helps some people with flakey adapters that drop the connection on a regular basis. While this is only a workaround hopefully it will make your adapter more usable until a permanent fix is found.
Note:
If you use suspend or hibernate this service could possibly interfere with proper suspension. However, tests on my system turned up no negative side effects from the service.
Please leave feedback if the script works well for you, (or even if it doesn't. It may simply require a little tweaking to get it to work properly on your system. Hopefully I can assist you to get it working if need be.