21

The wireless connection in my house unfortunately often disappears, requiring a wireless router reboot.

Making this worse is that my ubuntu media pc, does not automatically reconnect to the wireless network when it's been gone, and then comes up about a minute later. The network in question is setup as "connect automatically" in the network settings.

If I manually select my wireless network, using the wireless icon in the topright of my screen, everything works fine, until the next time that wireless goes down.

I'm looking for a way so I don't have to remember to do this manually all the time.

Jorge Castro
  • 71,754

9 Answers9

4

This seems to be posted all over the net with no good solutions. I guess the best fix/workaround is to make it check for internet connectivity and if it is not there then reconnect. I did this via a ping test to google.com and then I simply made it restart networking. Code is not tested (the restart part and the cron part, if statement tested), so I'll just wait for it to disconnect at some point. I have an Ubuntu Server 12.10, so no GUI, and is a pain to connect monitor and keyboard every time the wireless stuffs up.

Cron part done via webmin so Idk much about it. Script is as follows:

# edited by dim_voly for networking restart on no pingback every 5 mins

#!/bin/bash
# Name of File: networkingCron
# Purpose: to check if the internet is up (via ping test to google) and if not, restart networking service
# this script is invoked via cron, ideally every 5 mins.

#check if there is internet via ping test
if ! [ "`ping -c 1 google.com`" ]; then #if ping exits nonzero...
   sudo service networking restart #restart the whole thing
   echo Networking service restarted due to no ping response from google.com
fi

echo Script 'networkingCron' completed, if no message above then there was no network restart.

# dunno how to restart the wifi only since that is the only active connection that server uses.

# also I don't think those echos go anywhere

Make sure to run as root and make sure the script has execute (u+x) permissions.

links:

derHugo
  • 3,356
  • 5
  • 31
  • 51
dim_voly
  • 151
  • 1
  • 6
3

Just create a new file vi /root/checkwanup and add this content:

#!/bin/bash    
wlan=`/sbin/ifconfig wlan0 | grep inet\ addr | wc -l`    
if [ $wlan -eq 0 ]; then    
    /sbin/ifdown wlan0 && /sbin/ifup wlan0
else    
    echo interface is up    
fi

Then chmod 555 /root/checkwanup and add it to your crontab:

crontab -e
*/15 * * * * /bin/bash /root/checkwanup

Source: http://sirlagz.net/2013/01/10/script-wifi-checker-script/

derHugo
  • 3,356
  • 5
  • 31
  • 51
DougD
  • 71
  • 1
3

I had a similar problem with my laptop's Intel Wireless WiFi 5100 half height card and the driver iwlagn driver. This problem is a known issue with the iwlagn driver, and the best workaround is to disable 802.11n on the card.

To disable 802.11n on this card create/edit your /etc/modprobe.d/options.conf file:

sudo -H gedit /etc/modprobe.d/options.conf

And add the following to it.

options iwlagn 11n_disable=1 11n_disable50=1
Eliah Kagan
  • 117,780
Mike
  • 707
3

More modern version of @DougD script

#!/bin/bash    
wlan=$(/sbin/ifconfig wlan0 | grep inet\ addr -c)
if [ "$wlan" -eq 0 ]; then    
    /sbin/ifdown wlan0 && /sbin/ifup wlan0
else    
    echo interface is up    
fi
derHugo
  • 3,356
  • 5
  • 31
  • 51
hawk
  • 131
  • 2
3

This is an alternative using service network-manager restart:

#!/usr/bin/env bash


# 1. copy this script into
# /usr/bin

# 2. change permissions
# root:/usr/bin# chmod +x checkwifi.sh 

# 3. add to cron as root
# sudo su
# crontab -e

# add this to check your wifi every minute
# * * * * * /usr/bin/checkwifi.sh

is_ok=$(/sbin/ifconfig wlp2s0 | /bin/grep inet\ addr -c)

if [ "$is_ok" -eq 0 ] ; then

    # restart
    /usr/sbin/service network-manager restart

    # wifi is ok
    /bin/echo $(date) "wifi was restarted" >> /user/user/Dropbox/wifi.log
    /bin/echo $(/sbin/ifconfig wlp2s0) >> /home/user/Dropbox/wifi.log

else

    # wifi is ok
    /bin/echo $(date) "wifi is ok" >> /home/user/Dropbox/wifi.log
    /bin/echo $(/sbin/ifconfig wlp2s0) >> /home/user/Dropbox/wifi.log

fi
derHugo
  • 3,356
  • 5
  • 31
  • 51
auraham
  • 177
1

Another network manager solution, explicitly testing on a wifi connection (beware, there may be multiple adapters though, then adapt). Tested Ubuntu 22.04.

#!/bin/bash
numConnections=$(/usr/bin/nmcli con show --active | grep wifi | wc -l)

if [ $numConnections == 0 ] then echo "Wifi not connected, trying to restart" /usr/bin/nmcli radio wifi off # might not be necessary /usr/bin/nmcli radio wifi on else echo "Wifi still connected" fi

As above, add to (user) crontab, crontab -e. Add an adaptation of this line to check your wifi every minute (be sure to line break after the new line):

* * * * * /bin/bash /path/to/testIfWifiOnOrRenable.sh

Note, what you also should be aware of, is the power save behaviour for WiFi connections of Ubuntu. It can be activated/deactivated in /etc/NetworkManager/conf.d/default-wifi-powersave-on.conf, by setting

wifi.powersave = 2

instead of wifi.powersave = 3, see also https://unix.stackexchange.com/a/315400

DomTomCat
  • 111
1

You might want to have a look at using wpa_supplicant instead of network-manager, but that doesn't really matter when on a media-center. wpa_supplicant isn't as flexible as network-manager but afaik it doesn't give up after trying three times. have a look at this answer.

1

Here is my version - it works with NetworkManager:

#!/bin/bash    
wlan=`/sbin/ifconfig wlan0 | grep inet\ addr | wc -l`    
if [ $wlan -eq 0 ]; then   
    /usr/bin/nmcli nm wifi off && /usr/bin/nmcli nm wifi on 
fi
derHugo
  • 3,356
  • 5
  • 31
  • 51
Chris
  • 11
  • 1
0

All the scripted solutions are crappy because they are either very slow (1 minute delay, hmm, I'd rather start ifdown && ifup by hand than wait) or complex and buggy (I don't see such here btw).

The only normal solution is using iwd instead of wpa_supplicant. How you set it up depends on your needs. F.ex. you can set it as NetworkManager backend in /etc/NetworkManager/conf.d/wifi_backend.conf (be aware of bugs):

[device]
wifi.backend=iwd

From LWN article:

Iwd will be the only program that performs WiFi scanning on systems where it is running; that differs from systems using wpa_supplicant, where higher-level software must also scan for networks. With iwd, that work has all been pushed to a single level where good decisions can be made.

midenok
  • 792
  • 1
  • 8
  • 13