Schedule an automatic reboot for a Linux server

TL, DR

In order to schedule an automatic reboot on our Linux server we need to use SuperUser privileges. Using privileged Crontab we can automate privileged operations on Linux machines.

Need privileged automation?

Sometimes we need to schedule a periodic reboot for our Linux machine – physical or in the cloud. There is an easy way to do that using Crontab, which is included in all Linux distributions as a standard component.

However, regular Crontab cannot automatically execute commands which require higher privileges, like a reboot. Therefore, we need to use a “special” Crontab schedule, calling it with Superuser privileges. Commands to do so are:

sudo crontab -e

The first time you call this command it will prompt you to choose the text editor for editing the Crontab. I usually go with “1” for Nano. After this, you can append your line for the rebooting command:

3   1   *   *   *   /sbin/shutdown -r

This command will schedule an automatic reboot for your Linux machine every day at 1.03 AM. You can then use ctrl + o to write the new Crontab and ctrl+ x to exit (if you used the Nano editor). Crontab will confirm the successful installation of the new table, or prompt you to edit it in case it detect any syntax error.

The syntax of Crontab may appear difficult, but it becomes quite easy once you grasp the fundamentals. Each value indicates a different element (as you can also see in the Crontab page), and you can use both ranges and list of values. Elements are:

minute  hour  day(month)  month  day(week)

There are also “special” Crontab entries, like the following ones and more:

@reboot
@hourly
@daily
@weekly
...

You can also use one of the many online tools to check or create your Crontab entries, like Crontab Guru.

In summary, we showed how to schedule an automatic reboot for Linux servers. Hope you can find this useful for your projects!

Related links

Do you like our content? Check more of our posts in our blog!