Setting up reboot cron job in Ubuntu instance

   

Maintaining the stability and efficiency of a Linux server like Ubuntu requires periodic reboots to apply updates and clear system resources. While manual reboots are an option, automating the process using cron jobs can save time and ensure timely reboots, especially for servers that require high uptime. In this guide, we'll walk you through the process of installing cron jobs and configuring automatic reboots on an Ubuntu system.

Step 1: Install Cron

Before setting up cron jobs, ensure that the cron package is installed on your Ubuntu system. If not already installed, you can install it by running the following command in your terminal :

sudo apt update
sudo apt install cron
sudo systemctl enable cron

Step 2: Edit Cron Jobs

Once cron is installed, you can start setting up cron jobs. Open the cron jobs configuration file by running the following command :

sudo crontab -e

This command opens the default text editor (usually nano) with your user's cron jobs configuration file.

Step 3: Add Reboot Command

In the cron jobs editor, add the following line at the end of the file to schedule a reboot at a specific time, say 5:00 AM daily :

0 5 * * * /sbin/reboot

This cron job entry specifies that the system should reboot at 5:00 AM every day. You can adjust the time as needed by modifying the 0 5 part (where 0 stands for minute and 5 stands for hour). Refer to cron's syntax for more options if you need to set up a different schedule.

Step 4: Save and Exit

Once you've added the reboot command, save the changes and exit the editor. In nano, you can do this by pressing Ctrl + O to write the file, then Enter to confirm, and finally Ctrl + X to exit.

Step 5: Testing

It's important to test the automatic reboot to verify that it works as expected. You can simulate the reboot process by scheduling it a few minutes ahead by setting the clock settings on ubuntu. For example, you can set the clock on your Ubuntu 5 minutes before the time the cronjob is run, if the current time is 04:55, then the cron job will reboot at 05:00 for testing purposes.

0 5 * * * /sbin/reboot

Step 6: Monitor and Adjust

Once you've verified that the automatic reboot works correctly, monitor your server's performance to ensure it functions smoothly after reboots. You may need to adjust the reboot schedule based on your server's workload and usage patterns.

Conclusion

Automating Ubuntu reboots using cron jobs is a straightforward process that can enhance the stability and performance of your server. By following the steps outlined in this guide, you can set up scheduled reboots effortlessly, ensuring that your system remains updated and reliable. Remember to test thoroughly and monitor your server after implementing automatic reboots to maintain optimal performance.