DEV Community

Mayur
Mayur

Posted on • Originally published at mayurmadnani.hashnode.dev on

"Wifi Restart Service" to rescue intermittent disconnections

I started facing wifi issues on my laptop Acer Predator G3-572.

The router was TPLINK 300M Wireless N operating in bgn mixed mode. The laptop had a MIMO wireless chipset Qualcomm Atheros QCA61x4A Wireless Network Adapter. Just upon connecting with the router, in a rare 10 seconds all connected devices would get disconnected and no device can reconnect to the router again. Only restarting the router helps till I connect my device back😅. My initial thought was it was a router problem of somehow not being compatible with my newer wifi hardware. This was mentioned in many forums too but the router being too old, there were no firmware upgrades available. Strangely, Acer Nitro series laptops with Intel wifi chipset having MIMO support were working (I had Qualcomm chipset). I was vexed to fix the problem.

In Windows, from the driver manager, I figured I could set the wireless mode to any of a/b/g/n/ac. With mode 802.11b the disconnect issue had gone and I was connected without disruptions. But at a cost of slower speeds limited by the wireless mode. Upon switching to Linux, wireless mode switching was not an option as it was left at router negotiation and I was left with no choice but to get a USB wifi dongle. Soon, I realized that even the USB wifi had the same disconnect problems. Thankfully, it would stay connected for a longer duration compared to the internal WiFi module.

I created a startup service to rescue from the wifi issue with my laptop. The service will ping the router IP periodically and see if it gets the response. If not, wifi is disconnected and reconnected again. Having this taken care of, I was enjoying disruption-free browsing.

Assuming the below, you can use the same script for your needs

  1. SSID: MySSID

  2. Password: Password@123

  3. device: wlx01

Create a file /usr/bin/MySSID.sh

DATE=`date '+%Y-%m-%d %H:%M:%S'`
echo "MySSID service started at ${DATE}" | systemd-cat -p info
device="wlx01"
conn="MySSID"
router_ip="192.168.0.1"
while true
do
    ping -q -W1 -c1 ${router_ip}  >/dev/null
    if [ $? -ne 0 ]; then
        echo "MySSID disconnected- ${DATE}" | systemd-cat -p info
        nmcli connection down ${conn}
        sleep 3s
        nmcli connection up ${conn} passwd-file /usr/bin/MySSID.psk
        echo "MySSID connected- ${DATE}" | systemd-cat -p info
    fi
    sleep 10s
done
Enter fullscreen mode Exit fullscreen mode

Create file /usr/bin/MySSID.psk

802-11-wireless-security.psk:Password@123
Enter fullscreen mode Exit fullscreen mode

Create file /lib/systemd/system/MySSID.service

[Unit]
Description=MySSID systemd service.
[Service]
Type=simple
ExecStart=/bin/bash /usr/bin/MySSID.sh
[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Now that you have the service created.

Check status: sudo systemctl status MySSID.service

Start service: sudo systemctl start MySSID.service

Enable service: sudo systemctl enable MySSID.service

Disable service: sudo systemctl disable MySSID.service

To see how many times has the service restarted the wifi connection, search by the logs for the active boot journalctl -b | grep "MySSID disconnected"

With this service running, I was never disconnected for long.

Note: When the router was upgraded later, there were no wifi fixes to be done. Everything ran smoothly from the first day. This new router had dual-band support and was MIMO compatible. Something missed out earlier? 😉

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs