Ubuntu – How to restart every 30 minutes automatically

cronrestart

I would like to schedule a restart of my Ubuntu every 30 minutes. Is there any command or a graphical way to do it?

Best Answer

The best way to do this will depend on why you want Ubuntu to restart every half hour.

So I recommend editing your question to explain why you wish to do this.

Rebooting every 30 minutes, and warning users before each reboot:

Assuming people might be using the machine, either locally or remotely, it's best to avoid restarting Ubuntu from under them without any warning. Therefore, rather than scheduling the reboot command, I recommend scheduling the shutdown command so it warns the user.

To schedule a shutdown every half hour with a warning 5 minutes before, add this to /etc/crontab:

#minute hour    mday    month   wday    user    command
*/30    *       *       *       *       root    shutdown -r +5

You don't actually have to add the fist line, which is a comment. I've included it for clarity--something like it is there already.

  • This will schedule the system to go down for reboot (-r) five minutes after (+5) the command runs. It runs every at every half hour mark (*/30). See man cron and man 5 crontab.
  • Change +5 to something else to change how long users have after being warned of reboots.
  • 0,30 under minute will also work, if you prefer that. (Similarly, if it were every 20 minutes, you could write */20 or 0,20,40.)
  • Make sure /sbin is in the PATH variable specified near the top of /etc/crontab. Otherwise, shutdown (under command) will have to be invoked as /sbin/shutdown.

The command will always run on the half-hour mark, if the machine is up and running at that time. This will cause the shutdowns to be announced every half hour and performed at 5 minutes and 35 minutes past the hour.

  • One benefit here is that an administrator can cancel the just-announced shutdown with sudo shutdown -c.
  • If the computer is down during the specific times when the scheduled command is to be run, it will not run. If that's not adequate to your needs, you'll have to schedule your reboots differently. (This is not specific to the use of shutdown but would apply equally if you were scheduling reboot.) In that case, please edit your question to explain your specific needs. (I'd recommend anacron for this, but your time intervals are far too short.)

Making it easier for administrators to prevent automatic reboots from happening at all:

You might want to set this up so that it's easy for an administrator to suspend all automatically scheduled reboots:

#minute hour    mday    month   wday    user    command
*/30    *       *       *       *       root    [ -e /etc/noautoreboot ] || shutdown -r +5

This schedules reboots the same way--every half hour, with five minutes warning--except that it will not schedule a reboot if a file called noautoreboot exists in /etc.

  • This control file can be created by an administrator with:

      sudo touch /etc/noautoreboot
    
  • It can be deleted with:

      sudo rm /etc/noautoreboot
    
  • Note that it's whether or not the file exists, not what it contains, that matters.

  • If the reboot is scheduled and users are warned, then the file is created, the (immediately upcoming) reboot will still occur.

  • How does this work? It uses a short-circuit-evaluated or operator (||) as shorthand for:

    If /etc/noautoreboot doesn't exist, run shutdown -r +5.

    This answer explains how short-circuit and and or operators can perform if-then logic. For a brief, intuitive and highly informal explanation, you can read the command this way:

    /etc/noautoreboot exists! Or, run shutdown -r +5.

    See man [ to see how the test itself is performed.