9

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?

2
  • 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.
    – Sadi
    May 29, 2015 at 10:15
  • 1
    There are a number of bug reports that suggest there may be an issue. Could you post details of your kernel (uname -a) nd network manager version, and also details of your WiFi card.
    – DaveM
    May 29, 2015 at 11:17

3 Answers 3

7
+50

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

1

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.

1
  • The above solution is fine but how to catch the notification from network manager (nm) that the connection has dropped. This should be possible from within nm itself, a quick look through the source would seem to propose the funcionality is there. The message is taken from dbus and the nm icon updates, so it is possible this is a bug, or maybe another part of the system catches the drop of the connection and has been modified and the notice no longer passes through to attempt to reconnect?
    – DaveM
    May 29, 2015 at 10:45
0

What I think the issue is like the connection gets lost because of router's connectivity issues. Restarting the network manager couldn't help when the router itself is having issues. Rather you could create a script to put the process running the software to suspend using SIGSTOP when the connection gets lost, and again trigger to process to continue using SIGCONT when the network becomes available. And add the script to your cron jobs, scheduling it to run every minute.

For your reference : http://hints.macworld.com/article.php?story=20030915193440334

Hope this helps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .