At some point, you may need to restart your Linux server regularly for various reasons. For instance, you may want to ensure that the system runs smoothly and efficiently by refreshing the memory, closing unused applications, and releasing system resources. Restarting your server regularly also helps to apply updates and patches, which is critical to maintaining the security and stability of your system.
In this guide, we will show you how to restart your Linux server every midnight automatically. This process will ensure that your server stays up to date and performs at its best. The steps outlined in this guide are specific to Ubuntu and other Debian-based distributions.
Step 1: Install the cronjob utility
Cron is a time-based job scheduler in Unix-like operating systems. It enables users to schedule and automate tasks at specific intervals. To install the cronjob utility, open the terminal and type the following command:
sudo apt-get install cron
Enter your password when prompted, and the cronjob utility will be installed on your system.
Step 2: Create a new cronjob
To create a new cronjob, you need to edit the crontab file. Open the terminal and type the following command to open the crontab file:
sudo crontab -e
This command will open the crontab file in the default text editor. Scroll down to the bottom of the file and add the following line:
0 0 * * * /sbin/shutdown -r now
This line specifies the time and command that you want to run. In this case, the server will restart at midnight (0 0 * * *) every day using the shutdown command with the -r (restart) option.
Save the file and exit the editor. Your new cronjob is now set up and will automatically restart your server every midnight.
Step 3: Verify that the cronjob is running
To verify that the cronjob is running, you can check the system logs. Open the terminal and type the following command:
sudo grep CRON /var/log/syslog
This command will display the cron logs for the current day. You should see a log entry for the cronjob that you created, indicating that it ran successfully.
In this guide, we showed you how to restart your Linux server every midnight automatically using the cronjob utility. By automating this process, you can ensure that your server stays up to date and performs at its best. Remember to test your cronjob to make sure that it works correctly and to adjust the timing if necessary.
If you have any questions or concerns about this guide, feel free to contact us. We are always here to help you.