How to determine if the wifi is disconnected from the command line?

Ubuntu version: 20.04
Shell: bash
Kernel version: 5.8.0-53-generic #60~20.04.1-Ubuntu

I have a PC which runs a cryptocurrency workload 24/7, and it must be connected to wifi. I have noticed that at least once in a day, the wifi goes down and it does not automatically reconnect. The wifi icon just goes off with a question mark as shown below.

Wifi icon with question mark sign

I have to manually run a network-manager restart (as shown below) to fix the issue.

sudo service network-manager restart

Until I find a permanent fix I am thinking a running a script every 5 minutes which will check if the wifi is down, and if it is down it will run a network-manager restart. How do I determine if the wifi is down from command line?


Failing to find another solution, you can try something similar to this:

tail -f /var/log/syslog | grep --line-buffered ' wlo1: CTRL-EVENT-DISCONNECTED ' \
| while read line
do
  echo "Caught: $line"
  echo "Restarting network-manager..."
  sudo service network-manager restart
done

You can modify the string in grep to something that matches your system.


The command-line tool nmcli has a connectivity check with the below command:

    nmcli networking connectivity

From man pages

   connectivity [check]
       Get network connectivity state. The optional check argument tells NetworkManager to
       re-check the connectivity, else the most recent known connectivity state is displayed
       without re-checking.

       Possible states are:

       none
           the host is not connected to any network.

       portal
           the host is behind a captive portal and cannot reach the full Internet.

       limited
           the host is connected to a network, but it has no access to the Internet.

       full
           the host is connected to a network and has full access to the Internet.

       unknown
           the connectivity status cannot be found out.

When you run nmcli networking connectivity command, it returns any of the below values:

none, portal, limited, full, unknown

You can prepare your script according to the value you required.