System Automatic Restart

Method 1: Using Cron Jobs (Recurring Restarts)

Cron jobs allow you to schedule commands to run at specified intervals.

Setting Up a Daily Restart at 3:00 AM

# Edit the crontab file
sudo crontab -e

# Add this line to restart daily at 3:00 AM
0 3 * * * /sbin/shutdown -r now

Common Cron Schedule Patterns

Schedule Cron Expression Description
Daily at 1:00 AM 0 1 * * * /sbin/shutdown -r now Restarts every day at 1:00 AM
Every Sunday at 4:00 AM 0 4 * * 0 /sbin/shutdown -r now Restarts weekly on Sundays
Every 6 hours 0 */6 * * * /sbin/shutdown -r now Restarts every 6 hours

Cron Format Explained

MIN HOUR DAY MONTH DAYOFWEEK COMMAND

Method 2: One-Time Scheduled Restart

To schedule a one-time restart:

# Restart 24 hours from now
sudo shutdown -r +1440

# Restart at a specific time today
sudo shutdown -r 23:00

# Cancel a scheduled restart
sudo shutdown -c

Checking Scheduled Shutdowns/Restarts

# Check if a shutdown/restart is scheduled
cat /run/systemd/shutdown/scheduled

# Cancel a scheduled shutdown/restart
sudo shutdown -c

# Schedule a restart at specific time (e.g., 11:00 PM)
# sudo shutdown -r 23:00

# view the scheduled shutdown if there is any
cat /run/systemd/shutdown/scheduled

# View scheduled shutdown in human-readable time format
awk -F= '/^USEC=/ { print $2 }' /run/systemd/shutdown/scheduled | awk '{ print strftime("%Y-%m-%d %H:%M:%S", $1 / 1000000) }'

Method 3: Using systemd Timer (Modern Systems)

For systems using systemd, you can create a timer:

  1. Create a service file:
sudo nano /etc/systemd/system/restart.service

Add the following content:

[Unit]
Description=System Restart Service

[Service]
Type=oneshot
ExecStart=/sbin/shutdown -r now

[Install]
WantedBy=multi-user.target
  1. Create a timer file:
sudo nano /etc/systemd/system/restart.timer

Add the following content for a daily 3:00 AM restart:

[Unit]
Description=Daily System Restart Timer

[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true

[Install]
WantedBy=timers.target
  1. Enable and start the timer:
sudo systemctl enable restart.timer
sudo systemctl start restart.timer

Checking Your Scheduled Restarts

For Cron Jobs

sudo crontab -l

For Systemd Timers

systemctl list-timers

Additional Notes

# Restart at a specific time today
sudo shutdown -r 23:00

# Shutdown at a specific time today (no restart)
sudo shutdown -h 23:00