How to make a marginal WiFi connection automatically reconnect if it drops? - Ubuntu 16.04 LTS

Running Ubuntu 16.04 the WiFi-only computer has a marginal connection to the router. Usually it works for hours on end and even has worked for days.

It is using a rt2800usb external USB WiFi adapter (it claims 3000mW) with a 14 inch antenna on it. (Perhaps I need a yagi or a dish?)

I never use suspend; just lock. This is a computer located out in a workshop but I do use RDP to access it for various tasks (like monitoring the workshop and CPU temperature environment with an Arduino connected via USB).

To the problem at hand: Occasionally the connection drops. I'm not sure why but it will drop the connection entirely from time to time.

To fix it requires logging in out there and using the up/down icon to shut off Wireless then turn it back on. Then it will again go for hours or days working fine.

It would be preferable if it could reconnect itself when this happens.

The only other questions I see here on this subject are like 5 years old and for previous versions, or involving suspend. None match the conditions of this box.

So, the question is in the title. How can I set up 16.04 to automatically reconnnect when it drops off?


Solution 1:

You can use Network-Manager dispatcher scripts to achieve such behavior. You can find more info about those in the Ubuntu manpages. In short: you put a script, which is owned by root and executable, in the folder: /etc/NetworkManager/dispatcher.d.

If you plan on putting more such scripts in there, be aware, that they get executed in alphabetical order.

A script that starts a connection, if the same connection went down could look like this:

#!/bin/bash

if [ "$CONNECTION_UUID" = "put_your_uuid_here" ]; then
    if [ "$2" = "down" ]; then
        sleep 10
        nmcli con up uuid $CONNECTION_UUID
    fi
fi

Be aware, that you need to put the uuid of your connection in the script where it says put_your_uuid_here. To find your connections uuid you can run the command nmcli con show in a terminal.

Cheers