Automating server reboots ensures your server stays fast, secure, and stable. By using cron jobs, you can schedule reboots at regular intervals without manual intervention. This guide walks you through the process safely and clearly.
Table of Contents:
Understanding Cron Jobs
Cron is a Linux scheduler that runs tasks at specified times. Cron jobs are defined in a crontab file, which uses this format:
* * * * * command_to_execute - - - - - | | | | | | | | | +---- Day of the week (0 - 7, Sunday = 0 or 7) | | | +------ Month (1 - 12) | | +-------- Day of the month (1 - 31) | +---------- Hour (0 - 23) +------------ Minute (0 - 59)
For server reboots, the cron job will run the shutdown ‘-r now’ command at the scheduled time.
Steps to Schedule Automatic Server Reboots
Step 1: Open the Root Crontab
Run the following command to edit the root user’s crontab:
sudo crontab -e
This opens the crontab in your default editor (nano or vi).
Step 2: Add the Reboot Schedule
To reboot the server daily at 2:00 AM, add:
0 2 * * * /sbin/shutdown -r now
Explanation:
- 0 2 * * * > At 2:00 AM every day
- /sbin/shutdown -r now > Reboots the server immediately
Tip: Always use the full path /sbin/shutdown to avoid errors.
Step 3: Save and Exit
Cron applies changes automatically.
Verifying Your Cron Job
- List all scheduled cron jobs for root:
sudo crontab -
- Check server logs after reboot:
grep reboot /var/log/syslog # Ubuntu/Debian grep reboot /var/log/messages # CentOS/RHEL
Best Practices
- Schedule during off-peak hours to minimise impact.
- Notify users if it’s a shared or production server.
- Log cron job output to troubleshoot easily:
0 2 * * * /sbin/shutdown -r now >> /var/log/reboot.log 2>&1
- Avoid frequent reboots; restart only when necessary.
Troubleshooting
- Cron job not running?
- Check if the cron service is active:
sudo systemctl status cron
- Ensure full paths are used in commands.
- Check cron logs:
tail -f /var/log/cron
- Check if the cron service is active:
- Reboot failed?
- Test the command manually:
sudo /sbin/shutdown -r now
- Test the command manually:
A Linux VPS Hosting solution provides full root access and the flexibility needed to automate server administration efficiently.
Conclusion
In this manner, scheduling automatic server reboots with cron is a simple yet powerful way to maintain server stability, apply updates, and avoid downtime. By following this guide, you can safely automate reboots, minimise manual intervention, and ensure your server continues running smoothly.
Want to learn how to set up your own cron jobs from scratch? Follow the guide: How to Create a Cron Job in Linux