For a simple one-time reboot, nothing beats the classic shutdown
command:
sudo shutdown -r 02:30 "Server maintenance reboot"
This will schedule a reboot for 2:30 AM with the specified message broadcast to logged-in users.
The at
command provides finer granularity for scheduling one-time tasks:
echo "sudo reboot" | at 3:15AM tomorrow
Or for a specific date:
echo "sudo reboot" | at 11:45PM Oct 31
To check pending shutdowns:
shutdown -c
For at
jobs:
atq
For modern systems, create a one-shot timer:
sudo systemd-run --on-calendar="2023-12-25 00:00:00" --unit=reboot-timer reboot
Check active timers with:
systemctl list-timers
When working through SSH, add nohup
to prevent cancellation:
nohup sudo shutdown -r +30 &
- Always notify users before scheduling reboots
- Consider running
sync
before reboot commands - For production systems, implement proper service checks
While cron is perfect for recurring tasks, it becomes cumbersome when you need to schedule a one-time reboot. Manually adding and removing cron entries introduces unnecessary overhead and potential human error. Let's explore two better alternatives.
The simplest solution is using the at
command, specifically designed for one-time scheduled tasks:
# Schedule reboot at 2:30 AM tomorrow
echo "sudo reboot" | at 02:30
# Verify scheduled job
atq
# For a specific date and time
echo "sudo reboot" | at 02:30 2023-12-25
Key advantages:
- No persistent configuration needed
- Automatically cleans up after execution
- Simple syntax for one-time tasks
For modern Linux systems using systemd, we can create a transient timer:
# Schedule reboot in 2 hours
sudo systemd-run --on-active=2h --property=Description="One-time reboot" reboot
# Schedule at specific time (Jan 1 2024 at 3:15 AM)
sudo systemd-run --on-calendar="2024-01-01 03:15:00" --property=Description="New Year reboot" reboot
To verify scheduled timers:
systemctl list-timers --all
For more complex scenarios where you need to execute commands before rebooting:
cat > /tmp/pre_reboot.sh << 'EOF'
#!/bin/bash
# Put your pre-reboot commands here
systemctl stop apache2
/usr/local/bin/backup_script.sh
EOF
chmod +x /tmp/pre_reboot.sh
# Schedule with systemd including the pre-reboot steps
sudo systemd-run --on-calendar="2024-01-15 04:00:00" --property=Description="Monthly maintenance reboot" /bin/bash -c "/tmp/pre_reboot.sh && reboot"
Before implementing in production, always:
- Test in a staging environment
- Notify users well in advance
- Implement proper logging
- Consider maintenance windows
# Example logging wrapper
cat > /usr/local/bin/safe_reboot.sh << 'EOF'
#!/bin/bash
LOGFILE=/var/log/scheduled_reboot.log
echo "[$(date)] Starting scheduled reboot procedure" >> $LOGFILE
/path/to/pre_reboot_script.sh >> $LOGFILE 2>&1
echo "[$(date)] Initiating reboot" >> $LOGFILE
/sbin/reboot
EOF