Hello, friends. In this post, you will learn how to program a reboot on Linux. This post will also help newbies to learn some Crontab and use it for more complex tasks.
Scheduling a reboot can be a fairly simple task to do, but occasionally, it can take us surprised. Fortunately, for us, Crontab is there, and the process is easier than you think.
Because, thanks to Crontab, you can choose the exact time and frequency you want the task to be done.
Restart the system using the terminator
The first thing we need to know is how to reboot the system using the terminal. For this, there are several commands and options, but in summary you can use the reboot command and the shutdown command with the -r
option.
An example of this is as follows. If you want to reboot the computer using reboot
then run in terminal.
sudo reboot
Or as root user
reboot
But if you would like to use shutdown
which is more flexible, then
sudo shutdown -r now
As you can see, it is simple, now let’s do it with crontab.
How to schedule a reboot on Linux?
To put it very simply, Crontab is a simple text file that stores a list of commands to be executed at a time specified by the user.
So, thanks to Crontab you can schedule a reboot every time you want. It is simple indeed.
Let’s start.
First open a terminal from your system’s main menu and, with user permissions, execute the following
sudo crontab -e
As soon as you enter the user password, you will be prompted to choose a text editor to edit the file. In this case, I will pick nano because it is the easiest to use.
There you will be presented with the file in question so that you can set how often to reboot the computer.
It’s all in the command you are going to set. For example, if you want to schedule a daily reboot at 04:00 then add the following to the file.
0 4 * * * /sbin/shutdown -r
Allow me to explain:
- 0: minutes
- 4: hour
- *: day of month
- *: month
- *: day of the week
- command
So, the above command indicates that every day of the week, every month at 4:00 will execute the command /sbin/shutdown -r
which will reboot the system.
This is flexible, so you can change the time and frequency. For example, if you only want it to reboot on Sundays at that time
0 4 * * 7 /sbin/shutdown -r
Once you have the code ready, then exit the editor by pressing Ctrl + x
press Y
to save the changes and enter to exit the editor.
Now the reboot will be scheduled.
Conclusion
In this simple post, you learned how to schedule a reboot on Linux using Crontab. This way you will be able to delimit when you want a reboot, either for maintenance or for having a home server.