0

I have a desktop running Ubuntu Gnome 16.04. On reboot, sometimes (randomly) the network card disappears. That is, eth0 disappears from ifconfig, lspci, etc. I cannot bring back eth0 without a reboot in such situations. Natually, the machine is not connected to the Internet when this happens. Also, usually, a single reboot fixes the problem and the ethernet shows up again.

I don't have a clue about how I can start to debug this. However, I would like to have a script or service that checks if the ethernet was loaded (computer is connected to the Internet), and reboots if not. This script or service should run before any user logs in, because, sometimes I restart the machine remotely, and lose access to it if ethernet does not come up.

I have a script that checks if the Internet is connected. But I have been unsuccessful in getting it to run at the correct time. I have tried using crontab, but cron seems to be initialized before internet connects, (as the log file records); I tried placing an upstart script in /etc/init/, only to realize that Ubuntu now uses systemd and upstart is not recommended anymore. I also wrote a systemd unit file, and had mixed results (bootloop and nothing). What am I doing wrong?

Internet connectivity test script

#!/bin/bash

ping -c 1 8.8.8.8 > /dev/null
if [ $? -eq 0 ]; then
  echo "ok"
else
  reboot
fi

systemd unit file that caused bootloop

[Unit]
Description=Tests Internet connectivity. Reboots if not connected.
After=network.target syslog.target

[Service]
Type=forking
ExecStart=/usr/local/bin/internet_test.sh
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=InternetTest

[Install]
WantedBy=multi-user.target

systemd unit file that did nothing (even the service did not log as started)

[Unit]
Description=Tests Internet connectivity. Reboots if not connected.
After=multi-user.target

[Service]
Type=forking
ExecStart=/usr/local/bin/internet_test.sh
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=InternetTest
0

These two files did the trick:

internet_test.sh (tests whether computer is online)

#!/bin/bash

/usr/bin/nm-online -q --timeout=30

if [ $? -eq 0 ]; then
  echo "ok"
else
  reboot
fi

systemd unit file

[Unit]
Description=Tests Internet connectivity. Reboots if not connected.
After=NetworkManager.service

[Service]
Type=oneshot
ExecStart=/usr/local/bin/internet_test.sh
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=InternetTest
RemainAfterExit=no

[Install]
WantedBy=multi-user.target

Thanks to the documentation and perserverence.


Warning: The user might still be able to log in before nm-online times out. Then the computer will rudely reboot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.