How to Schedule Daily Reboots on CentOS Using Cronjobs


Regular system reboots can help maintain the stability and performance of your CentOS server by clearing out temporary files, freeing up system resources, and applying necessary updates. Automating this process ensures that your server stays up to date and operates smoothly. In this guide, we'll walk through the steps to set up a cronjob to reboot your CentOS machine at a specific time every day.

Step 1: Access the Terminal

To begin, you'll need to access your CentOS server via SSH or directly through the terminal if you're working on a local machine.

Step 2: Edit the Crontab File

Cron is a time-based job scheduler in Unix-like operating systems. It allows users to schedule tasks to run periodically at fixed times, dates, or intervals. We will use cron to schedule the daily reboot of our CentOS machine.

Open the crontab file using your preferred text editor. For example, you can use the following command to edit the crontab file using the nano text editor :

sudo crontab -e

Step 3: Schedule the Reboot

Once the crontab file is open, add the following line at the end to schedule the reboot :

0 3 * * * /sbin/reboot

In this line:

- "0 3 * * " specifies the time at which the reboot will occur. In this example, the server will reboot every day at 3:00 AM. You can adjust the time according to your preference by modifying these values. The first value represents the minute (0), and the second value represents the hour (3). The asterisks () represent any value, meaning that the reboot will occur every day.

- "/sbin/reboot" is the command that will be executed. This command instructs the system to reboot.

Step 4 : Save and Exit

After adding the reboot command to the crontab file, save your changes and exit the text editor. In nano, you can do this by pressing Ctrl + X, then Y to confirm the changes, and Enter to save the file.

Step 5 : Verify the Cronjob

To ensure that the cronjob is set up correctly, you can list the cronjobs for the root user :

sudo crontab -l

You should see the line you added specifying the reboot command.

Step 6 : Testing

Wait for the scheduled time, and the server should automatically reboot. You can monitor the process using system monitoring tools or by logging back into the server after the scheduled time.

Conclusion :

Automating daily reboots of your CentOS server using cronjobs is a straightforward way to ensure system stability and performance. By following the steps outlined in this guide, you can schedule regular reboots at a specific time, keeping your server up to date and running smoothly. Remember to adjust the reboot time according to your server's maintenance window and user activity to minimize disruption.