在 Ubuntu 中,定时重启通常通过 cron 或 systemd timer 来实现。下面给你几种常见、可靠的方法。
定时重启必须用 root 权限:
sudo crontab -e
例如:
0 3 * * * /sbin/reboot
0 4 * * 0 /sbin/reboot
0 3 1,15 * * /sbin/reboot
cron 会自动生效,无需重启服务。
✅ 查看已有任务:
sudo crontab -l
有时推荐用 shutdown 而不是 reboot,可以让系统正常关闭服务:
0 3 * * * /sbin/shutdown -r now
-r 表示重启,now 表示立即执行。
sudo vim /etc/systemd/system/reboot-task.service
内容:
[Unit]
Description=Scheduled Reboot
[Service]
Type=oneshot
ExecStart=/sbin/reboot
sudo vim /etc/systemd/system/reboot-task.timer
示例(每天 3:00):
[Unit]
Description=Daily Reboot Timer
[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now reboot-task.timer
✅ 查看状态:
systemctl list-timers
sudo shutdown -r 03:00
或 30 分钟后:
sudo shutdown -r +30
确保 cron 正在运行
sudo systemctl status cron
不要频繁重启,避免影响服务
重要数据请先保存
如果是云服务器,确认控制台不会阻止重启
如果你愿意,可以告诉我:
我可以帮你写一份完全合适的配置。