Automatically Reconnecting WiFi on Openwrt

Hello ,
I want to add the following script ( wifi_rebooter.sh ) in /usr/local/bin ,
and make it executable by running!

chmod +x /usr/local/bin/wifi_rebooter.sh

schedule in /crontab

*/5 * * * * root /usr/local/bin/wifi_rebooter.sh

& I do not know how there is a solution to that ?!

Wifi_rebooster script

#!/bin/bash

# The IP for the server you wish to ping (8.8.8.8 is a public Google DNS server)
SERVER=8.8.8.8

# Only send two pings, sending output to /dev/null
ping -c2 ${SERVER} > /dev/null

# If the return code from ping ($?) is not 0 (meaning there was an error)
if [ $? != 0 ]
then
    # Restart the wireless interface
    ifdown --force wlan0
    ifup wlan0
fi

Thanks ..

The preformat button </> should be helpful to you

Managing the wireless interfaces, assuming you really want to "bounce" them every 5 minutes, is perhaps better done with wifi down and wifi up.

jeff@office:~$ wifi --help
Usage: /sbin/wifi [config|up|down|reload|status]
enables (default), disables or configures devices not yet configured.

Checking for wireless connectivity is perhaps better done with iw dev <name of wlan device> station dump or, even better, a ubus call to get associated stations.

cron works pretty much like any other `nix system.

1 Like