How to restart every 30 minutes automatically?

Solution 1:

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.

Solution 2:

I love doing this by telling the Session Manager we want to reboot. This can be done without root permissions, and we get a nice window that warns us that the system is going to be rebooted -even we can cancel the reboot if we like to.

The Graphical Way - Preferred Method

Install gnome-schedule from the Ubuntu Software Center. If you don't want to install anything additional, do it the Terminal Way.

Open gnome-schedule from the dash, create a new repeated task, and set these options:

  • Description: Whatever you want
  • Command: dbus-send --print-reply --dest="org.gnome.SessionManager" /org/gnome/SessionManager org.gnome.SessionManager.Reboot
  • Choose X Application just below the command.
  • Time and Date, Advanced:
    • Minute: 0,30

Leave the other options at their default values. Click on Add.


The Terminal Way - No Need for Additional Software

Run from the terminal:

crontab -e

Add this line:

0,30 * * * * DISPLAY=:0 dbus-send --print-reply --dest="org.gnome.SessionManager" /org/gnome/SessionManager org.gnome.SessionManager.Reboot

Save & exit. Assuming you are using nano (the default one), press Ctrl+o and Ctrl+x.

Please notice this won't work if your DISPLAY is actually different from :0, and that's the reason this method is not preferred. But, honestly, if you are rebooting your computer every 30 minutes, your DISPLAY will most probably always be :0.

Not using Gnome or Unity?

Both methods explained above depends on some gnome components, found both on Gnome sessions and on Unity. If you want to do this on another environments (such as Kubuntu's KDE, Kubuntu's LXDE...) you better replace the command with this one instead:

dbus-send --system --print-reply --dest="org.freedesktop.ConsoleKit" /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Restart

This won't ask for confirmation, and will restart inmediatly, but will work on all environments, assuming you have not manually uninstalled ConsoleKit, of course.

Solution 3:

Run sudo crontab -e from the command line and add this line to the file:

0,30 * * * * reboot

This tells the system to run the command reboot every 30 minutes as root. For an overview of the time syntax, see here: http://linuxmoz.com/crontab-syntax-tutorial/