Linux – Auto-Reboot and Shutdown with Cron Jobs – Task Scheduler alternative How to configure a daily or weekly automatic shutdown & reboot of your machine using cron job in Linux CentOS, Ubuntu, Debian, RHEL

Linux - Come inviare E-Mail con sSMTP (con configurazioni-tipo per GMail, Aruba e Yahoo)

Since we just published a guide to setup an auto-reboot or auto-shutdown on Windows machines, we thought that it could be useful to do the same for Linux systems as well.

The Linux built-in counterpart of the Windows Task Scheduler is called cron and it’s available on all the major distributions – CentOS, Ubuntu, Debian, RHEL and so on. As we can expect from Linux, instead of the fancy GUI provided by Windows we can set up our tasks with a simple and practical configuration file – which you will find in the following path:

The great thing about the crontab file is that it contains an awesome comment-based tutorial which will instruct you on everything at a glance:

As we can see from the last line of the above code snippet, we did setup a system auto-reboot on every sunday at 00:00 AM (midnight) using the /sbin/shutdown command, which is the exact counterpart of the Windows shutdown.exe tool.

Cron web-based GUI

If you’re looking for a fancier way of doing things, you can also setup your cron jobs with crontab.guru, a great web-based tool that allows to do that with a nice graphical interface. This tool can be very useful for beginners because it auto-calculates the timing options and translates them in a human-readable way, thus avoiding unwanted errors. Cron job failures can be disastrous, the website says in a tagline right below the GUI, and it’s definitely right!

Linux - Auto-Reboot and Shutdown with Cron Jobs - Task Scheduler alternative

Troubleshooting

Cron jobs are great, but they come with a minor flaw that can make the life hard for Linux newcomers: the cron service runs off the hardware clock rather than system clock, which can lead to minor or major disasters depending on your configuration and time zone settings, such as:

  • Cron jobs running at incorrect time, usually UTC instead of your local timezone.
  • Cron jobs running each and every minute – an edge-case scenario which happend to me some weeks ago, which I’ll describe in another post.

… And so on.

If you are experiencing this, check out your hardware clock by typing hwclock, then your system clock by typing date: if they do differ, you definitely have an out-of-sync issue which you should properly address.

The best thing you can do to fix that is to synchronize your hardware clock with your system clock using the following command:

Or, alternatively:

Not bad, right? However, since such sync might go away during time, it could be even better to periodically perform a resync. Ironically, the best thing to do that is using cron itself!

Here’s a crontab file that synchronizes the hardware clock with the system clock each day at midnight and performs a reboot at 00:01 am each sunday:

Conclusion

That’s it for now: I hope that this small tutorial will help other System Administrator looking for a way to setup an automatic shutdown and/or reboot on Linux. In case you need to do the same on Windows machines, check up this post!

 

 

 

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

0 Comments on “Linux – Auto-Reboot and Shutdown with Cron Jobs – Task Scheduler alternative How to configure a daily or weekly automatic shutdown & reboot of your machine using cron job in Linux CentOS, Ubuntu, Debian, RHEL

  1. Pingback: Windows - Auto-Reboot and Shutdown with Task Scheduler

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.