How to persist on a wifi connection after disconnection?

I've got a Ubuntu machine running a software that requires internet. However, sometimes the wifi fails (router reboot for example) and doesn't find the network again, until I manually service network-manager restart or I switch off and on the wifi to find it.

Is there a possible way to run a cron job or a supervisord job to monitor the connectivity and if the network can't be found to restart the network-manager service and connect to that specific SSID?


I think this might be due to a new network-manager bug, as I've also started experiencing this problem only recently - I think after upgrading to Ubuntu 15.04.

It seems I've managed to solve this problem temporarily as a workaround with these two files:

  1. I have a script file to watch the WiFi connection and toggle WiFi off and on when disconnected, e.g. ~/.bin/WiFi-Keep-Connected, with this content:


#!/bin/bash
while true; do
    LC_ALL=C nmcli -t -f TYPE,STATE dev | grep "^wifi:disconnected$"
    if [ $? -eq 0 ]; then
        rfkill block wifi && rfkill unblock wifi
        sleep 10
    fi
    sleep 10
done
  1. I also have a .desktop file to keep this script running every time I login, e.g. ~/.config/autostart/KeepWifiConnected.desktop, with this content:


[Desktop Entry]
Type=Application
Exec=/home/"username"/.bin/WiFi-Keep-Connected
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=Keep WiFi Connected
Icon=networkmanager

Note: You'll need to enter your user name in the path given for Exec= above.

NB: Sometimes restarting Wi-Fi in this way is not enough, in which case I have to restart Network Manager by entering this command once or even twice from the terminal: sudo service network-manager restart

Therefore, it might be a better idea to modify the script above (just in case) like this:


#!/bin/bash
while true; do
    LC_ALL=C nmcli -t -f TYPE,STATE dev | grep "^wifi:disconnected$"
    if [ $? -eq 0 ]; then
        rfkill block wifi && rfkill unblock wifi
        sleep 10
    fi
    LC_ALL=C nmcli -t -f TYPE,STATE dev | grep "^wifi:disconnected$"
    if [ $? -eq 0 ]; then
        gksudo service network-manager restart
        sleep 10
    fi
    sleep 10
done

I don't know why, but as you mention, sometimes when wifi fails, network-manager does not find the network again.

I think that the problem is that network-manager, after a wifi connection failure, does not refresh the wifi list (and it seems that network-manager hides the the problematic network). Network-manager does not have a re-scan menu option, and disabling and enabling wireless checkbox on network-manager menu does not force a new wifi scan (as restarting the service does).

When network-manager loses my wireles connection and it does not find my wifi again (and I known that the wifi is operative), I execute the command:

sudo iwlist wlan0 scan

It performs a wifi scan on wlan0 interface (wlan0 is the name of the wireless interface in my laptop) and it shows you the available wireless networks, but as a side effect network-manager automatically refresh its list and finds the lost network.

I prefer to execute the command manually when this problem happens (network reconnecting does not ensure comunication resuming, and some programs will need to be restarted).

But, as you mention, you can cron it, and you can do it without testing connectivity (the scan process updates your wifi list but it does not close your current wireless connection, if you are connected).

If you prefer you can test the conectivity using ping or iwconfig and if you are not connected then launch the scan.

Something like:

#!/bin/bash
if ! ping -c 1 -W 1 your_router_ip &> /dev/null
then
    iwlist wlan0 scan
fi

But remember that this script must be sudoed or executed with root privileges. It sends only 1 ping with 1 second timeout.