When working with Linux system administration, you'll commonly encounter several cron-related directories:
/etc/cron.d/
- For system-wide cron jobs/etc/cron.daily/
- Daily scripts/etc/cron.weekly/
- Weekly scripts/etc/cron.monthly/
- Monthly scripts
The standard crontab
command might not be immediately available if you're not using a personal user account with cron privileges. Let's first check if cron is installed:
# Check cron service status
sudo systemctl status cron
# Or for newer systems:
sudo systemctl status crond
Since you have access to cron.d
, we'll create a system-wide job. Here's how to schedule a reboot every 3rd Saturday at 23:30:
# Create a new cron job file
sudo nano /etc/cron.d/monthly-reboot
# Add this content:
30 23 * * 6 [ $(date +\%d) -le 21 ] && [ $(date +\%d) -ge 15 ] && /sbin/shutdown -r now
# Save and exit (Ctrl+X, Y, Enter)
The complex-looking command actually combines several elements:
30 23 * * 6
- Runs at 23:30 every Saturday[ $(date +\%d) -le 21 ]
- Checks if day of month ≤ 21[ $(date +\%d) -ge 15 ]
- Checks if day of month ≥ 15&& /sbin/shutdown -r now
- Reboots if both conditions are true
For systems that might be powered off during the scheduled time, consider using anacron:
# Create a custom anacron job
sudo nano /etc/anacrontab
# Add this line (runs with 7-day frequency):
@monthly 15 monthly-reboot /sbin/shutdown -r now
To confirm your cron job is set up correctly:
# List all cron jobs
sudo cat /etc/cron.d/monthly-reboot
# Check cron logs (timing may vary by distro)
sudo tail -f /var/log/cron.log
# Or alternatively:
sudo journalctl -u cron
When setting up automated reboots:
- Ensure critical services have auto-start configurations
- Consider notifying logged-in users before reboot
- Document the scheduled reboot in your system documentation
- For production systems, implement maintenance windows
If your reboot isn't executing as expected:
- Verify cron service is running:
sudo systemctl start cron
- Check file permissions:
sudo chmod 644 /etc/cron.d/monthly-reboot
- Ensure proper PATH settings in your cron job
- Test with a non-reboot command first
For more complex scheduling needs, consider creating a bash script in /usr/local/bin/
and calling that from your cron job instead.
When administering Linux systems, scheduling regular reboots is a common maintenance task. In this case, we need to configure a reboot every 3rd Saturday of the month at 23:30. While this might sound specific, cron (the time-based job scheduler in Unix-like systems) can handle it perfectly.
Many new Linux users get confused about cron's file structure. You mentioned seeing these directories:
cron.d
cron.daily
cron.weekly
cron.monthly
These are system cron directories, but for user-specific cron jobs, we need to use crontab
. Try running:
which crontab
If it's not found, you may need to install the cron package first with:
sudo apt-get install cron # For Debian/Ubuntu
sudo yum install cronie # For CentOS/RHEL
The magic happens in the crontab file. To edit your crontab:
crontab -e
For your specific requirement (3rd Saturday at 23:30), the entry would be:
30 23 * * 6 [ $(date +\%d) -le 21 ] && [ $(date +\%d) -gt 14 ] && /sbin/reboot
Let's break this down:
30 23 * * 6
- Runs at 23:30 every Saturday[ $(date +\%d) -le 21 ]
- Day of month is 21 or less[ $(date +\%d) -gt 14 ]
- Day of month is greater than 14&& /sbin/reboot
- If both conditions are true, execute reboot
Since you mentioned having cron.d, you could also create a file in /etc/cron.d/ (requires root):
sudo nano /etc/cron.d/monthly-reboot
Add this content:
30 23 * * 6 root [ $(date +\%d) -le 21 ] && [ $(date +\%d) -gt 14 ] && /sbin/reboot
After setting up, verify with:
crontab -l
For system-wide cron jobs in cron.d:
cat /etc/cron.d/monthly-reboot
Before deploying, test your logic with:
# Simulate 3rd Saturday:
date -d "2023-11-18" +"Day: %d, Weekday: %a"
[ $(date -d "2023-11-18" +\%d) -le 21 ] && [ $(date -d "2023-11-18" +\%d) -gt 14 ] && echo "Would reboot now"
# Simulate non-3rd Saturday:
date -d "2023-11-11" +"Day: %d, Weekday: %a"
[ $(date -d "2023-11-11" +\%d) -le 21 ] && [ $(date -d "2023-11-11" +\%d) -gt 14 ] && echo "Would reboot now" || echo "No reboot"
- Always use full paths to commands in cron jobs
- Consider logging:
> /var/log/reboot.log 2>&1
- For systemd systems, you might alternatively use systemd timers
- Make sure cron service is running:
sudo systemctl status cron