Automatically restart Wi-Fi when there are no clients connected.
Ever since I upgraded my router’s (TP-Link Archer C7 v2) firmware from OpenWRT Chaos Calmer 15.05.1 to LEDE 17.04.1, the 2.4G Wi-Fi access point has been dropping off every few days. The 5G however has been fine. With the older firmware however, the router could sustain for months without issues on 2.4G but 5G would need to be restarted every few weeks.
I needed a workaround that will restart the wireless adapter when there are no clients connected to it. This works for me because there are at least a few devices that are always connected 24/7.
Firstly, identify a list of Wi-Fi devices via command line:
A list of Wi-Fi devices will appear, along with its details such as ESSID, Access Point, Tx-Power, Signal, Bit Rate, Enryption, etc.
In my case, there are
wlan1 (2.4G) and
wlan1-1 (2.4G Guest).
To list clients that are connected to a device, the syntax is:
iwinfo <device> assoclist
In my case, the 2.4G non-guest is the one that I would like to monitor:
iwinfo wlan1 assoclist
If there are clients that are connected, they will appear along with MAC address, RX and TX rates, etc.
However, if there are no clients connected, the following message will appear:
No station connected
With this information, what I now need is a script that looks for this output and if found, it restarts the Wi-Fi.
I wrote a script that will restart Wi-Fi if there are no clients connected to a specific device, my 2.4G non-guest.
#!/bin/sh wlan1_assoclist="$(iwinfo wlan1 assoclist)" if [ "$wlan1_assoclist" == "No station connected" ] then logger Restarting Wi-Fi /sbin/wifi fi
Be sure to substitute
wlan1 with the adapter that you intend to monitor.
I placed the script into
Via LuCI, navigate to
System > Scheduled Tasks.
Add the following line into it crontab:
*/2 * * * * /opt/restart-wifi.sh
This causes the script to be executed every 2 minutes. More information on Cron syntax.
For each run of the script, a line will appear in syslog. If restart is required, the message “Restarting Wi-Fi” will appear in syslog.