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:
1 |
/etc/crontab |
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:
1 2 3 4 5 6 7 8 9 10 11 |
# For details see man 4 crontabs # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed 0 0 * * 0 root /sbin/shutdown -r now # reboot every sunday at 00:00 am |
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!
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:
1 |
# hwclock -w |
Or, alternatively:
1 |
# hwclock --systohc |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # For details see man 4 crontabs # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed 0 0 * * * root /sbin/hwclock -w # synchronize hardware & system clock each day at 00:00 am 1 0 * * 0 root /sbin/shutdown -r now "weekly reboot" # reboot every sunday at 00:01 am |
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!
Very good article thank you but how to do this same thing as a non root user?
I don’t think a non-root can alter cron jobs this way :) Sorry