Can I automatically reboot if disconnected from the internet?

12,504

You didn't mention the operating system, but if you're using Windows, something like this may be helpful: JScript for Ping, Renew IP and Network Info / Repair.

This is a script I put together for a similar issue. It does pretty much exactly what you're looking for, in that it pings the default gateway (which will be your router) on a given schedule, and then takes required action if the ping fails.

In this case, the problem PCs didn't need to be rebooted, but simply have an ipconfig /release and ipconfig /renew scripted in order to regain connectivity. Something like this might work well for your case as well. If a full reboot is still required, this script could be easily modified to account for that as well.

For Linux, you could use a shell script like this:

#!/bin/sh

ping -c5 192.168.1.1

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

This works because of the return code of ping, as detailed in the man page:

If ping does not receive any reply packets at all it will exit with code 1. If a packet count and deadline are both specified, and fewer than count packets are received by the time the deadline has arrived, it will also exit with code 1. On other error it exits with code 2. Otherwise it exits with code 0. This makes it possible to use the exit code to see if a host is alive or not.

After replacing 192.168.1.1 with the address of your router, you could schedule this to run every hour or so using cron. In order to reboot, you'll need to run this as root. For default Ubuntu, you can add a cron job using:

sudo crontab -e

To have this check run every hour, for example:

00 * * * * /path/to/script/pingRouterCheckReboot.sh

You'll need to ensure that the script is executable (chmod +x).

If you find that bringing the interface down and back up is sufficient, you could do this instead, doing something like ifdown eth0; ifup eth0. Or, combine approaches: First do this, and then do the ping test again. If connectivity still isn't restored, then force the drastic measure of a full reboot.

Share:
12,504

Related videos on Youtube

ganesh
Author by

ganesh

Updated on September 18, 2022

Comments

  • ganesh
    ganesh almost 2 years

    I have an old desktop that I'm running as a web server from my home while I'm at college. The internet at home is crappy and a lot of times when it goes down, the desktop can't reconnect without a reboot.

    Is there a way to make a script that loads on startup and every 12 hours or so, will attempt to ping the router, and if it fails, reboots the server?

    EDIT: Ok, I was able to make this script after googling around a bit.

    #!/bin/bash
    
    ping -c 1 192.168.1.1 > /dev/null
    if [ $? -ne 0 ]; then
      reboot
    fi
    

    That tests if the ping is successful, and if it's not it reboots.
    Now what do I need to do to make this run on a set schedule?

    • Admin
      Admin over 12 years
      If you're having to reboot the server, and not the router - the problem isn't with your Internet connection that you like so much. You've got other problems that need to be addressed on your desktop / server - and additional scripting likely isn't going to help.
    • Admin
      Admin over 12 years
      Ya I know there are other problems that need to be addressed, but I'm leaving in under 24 hours and I need a quick fix that will work until I come back home. I know it's not a perfect solution, but at least so far rebooting it has been working. And anyways, the server is pretty much only being used for personal testing use so a crappy little fix like this is fine for now.
    • Admin
      Admin over 12 years
      What OS are you using?
    • Admin
      Admin over 12 years
      Ubuntu server 11.10
  • Admin
    Admin over 12 years
    Oh sorry, I'm running ubuntu server. Although ya, I might be able to do a similar release and renew if it's down.
  • ziesemer
    ziesemer over 12 years
    @user1056869 - Please see my updates in the answer for a Linux / Ubuntu solution as well.
  • Admin
    Admin over 12 years
    Oh thanks, that should perfectly. I'll try that out.