
In this article, we will dive into the process of scheduling automatic reboots in Ubuntu using the Terminal or Bash file. This is a useful task for system administrators who need to ensure their systems are regularly refreshed and updated.
To schedule an automatic reboot in Ubuntu using the Terminal or a Bash file, you can use the crontab
command. There are two approaches you can take: using the user’s crontab or using the root’s crontab. In both cases, you can specify the time and frequency of the reboot using cron syntax. Remember to test any changes made to your system to ensure they work as expected.
Understanding Crontab
Before we begin, it’s important to understand the tool we’ll be using: crontab
. Crontab, or ‘cron table’, is a job scheduler in Unix-like operating systems. Users can schedule jobs (commands or scripts) to run periodically at fixed times, dates, or intervals.
Scheduling Automatic Reboot
Approach 1: Using User’s Crontab
- Open the Terminal: You can do this by pressing
Ctrl + Alt + T
or by searching for ‘Terminal’ in your system’s application launcher. - Edit the Crontab Entries: Type
crontab -e
to edit the crontab entries for the currently logged-in user. This will open the crontab file in your default text editor. - Add the Reboot Command: Add the following line to the crontab file, replacing
$PASSWD
with your password:
This line will execute the0 3 * * * echo $PASSWD | sudo -S reboot
reboot
command at 3 AM every day. The five fields represent minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), and weekday (0-7, 0 or 7 = Sunday), respectively. - Save and Exit: Save the file and exit the editor. The changes will take effect immediately.
- Verify the Changes: To verify that the new crontab entry is added, type
crontab -l
in the terminal. The new entry should be listed.
Note: This approach requires you to enter your password in the crontab file, which is not recommended for security reasons. If you are concerned about security, consider using the second approach.
Approach 2: Using Root’s Crontab
- Open the Terminal: Press
Ctrl + Alt + T
or search for ‘Terminal’ in your system’s application launcher. - Edit the Root’s Crontab Entries: Type
sudo crontab -e
to edit the root user’s crontab file. This will open the crontab file in your default text editor. - Add the Reboot Command: Add the following line to the crontab file:
This line will execute the0 3 * * * /sbin/reboot
/sbin/reboot
command at 3 AM every day. The five fields represent the same units of time as in the previous approach. - Save and Exit: Save the file and exit the editor. The changes will take effect immediately.
- Verify the Changes: To verify that the new crontab entry is added, type
sudo crontab -l
in the terminal. The new entry should be listed.
Note: In this approach, the crontab entry is added to the root user’s crontab file, which allows the reboot command to be executed without the need for a password. This is a more secure approach compared to the first one.
Providing a Warning Before Reboot
If you want to provide a warning before the reboot, you can modify the command to include a time delay. For example, you can use the following command to provide a 5-minute warning before the reboot:
0 3 * * * /sbin/shutdown -r +5 'System will reboot in 5 minutes'
This command uses the /sbin/shutdown
command with the -r
(reboot) option and a time argument of +5
, which means “5 minutes from now”. The text in the quotes is the warning message that will be broadcast to all logged-in users.
Conclusion
Scheduling automatic reboots in Ubuntu can be a simple yet effective way to keep your system running smoothly. By using the crontab
command, you can easily set up a schedule that fits your needs. Remember to always test any changes made to your system to ensure they work as expected.
For more information about crontab
, you can check the Ubuntu man page or the CronHowto guide on the Ubuntu community help wiki.
Yes, you can schedule automatic reboots for specific days of the week by modifying the day field in the crontab entry. For example, to schedule a reboot every Monday at 3 AM, you can use the following line in the crontab file: 0 3 * * 1 /sbin/reboot
. The number 1 represents Monday (0 represents Sunday).
To cancel or remove a scheduled reboot, you need to edit the crontab file and remove the corresponding entry. If you used approach 1 and scheduled the reboot in the user’s crontab, you can open the crontab file with crontab -e
and delete the line containing the reboot command. If you used approach 2 and scheduled the reboot in the root’s crontab, you can open the crontab file with sudo crontab -e
and remove the line with the reboot command. Remember to save the file after making the changes.
Yes, you can schedule reboots at different times on different days by adding multiple crontab entries. Each entry will specify a different time and day for the reboot. For example, if you want to schedule a reboot at 3 AM on Mondays and Wednesdays, you can add two separate lines to the crontab file: 0 3 * * 1 /sbin/reboot
and 0 3 * * 3 /sbin/reboot
.
You can check the last reboot time of your Ubuntu system by using the uptime
command in the terminal. Simply open the terminal and type uptime
. The output will display the current time, how long the system has been running, and the time of the last system reboot.
Yes, you can schedule reboots at specific times other than on the hour by modifying the minute and hour fields in the crontab entry. For example, if you want to schedule a reboot at 8:30 PM every day, you can use the following line in the crontab file: 30 20 * * * /sbin/reboot
. The number 30 represents the minute (30 minutes past the hour), and 20 represents the hour (8 PM).
Yes, you can schedule reboots on a monthly basis by modifying the month and day fields in the crontab entry. For example, if you want to schedule a reboot on the 1st day of every month at 2 AM, you can use the following line in the crontab file: 0 2 1 * * /sbin/reboot
. The number 1 represents the day (1st day of the month), and the asterisks (*) represent any month and any weekday.
If you schedule multiple reboots at the same time in the crontab file, all the scheduled reboots will be executed simultaneously. This can cause system instability and should be avoided. Make sure to review and manage your crontab entries to avoid overlapping schedules.