在 Ubuntu 中,最常用、最可靠的“定时重启”方式是使用 systemd 定时器(systemd timer) 或传统的 cron。下面分别说明。
sudo crontab -e
例如:每天凌晨 3 点重启
0 3 * * * /sbin/reboot
0 4 * * 0 /sbin/reboot
0 */6 * * * /sbin/reboot
⚠️ cron 使用的是系统本地时间
⚠️ 必须写/sbin/reboot或使用systemctl reboot
sudo nano /etc/systemd/system/reboot-task.service
内容:
[Unit]
Description=Reboot System
[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl reboot
sudo nano /etc/systemd/system/reboot-task.timer
每天 03:00 重启示例:
[Unit]
Description=Daily Reboot Timer
[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl daemon-reexec
sudo systemctl enable --now reboot-task.timer
systemctl list-timers
sudo shutdown -r 03:00
或
sudo at now + 6 hours
> reboot
> Ctrl+D
如果你说明:
我可以帮你直接写完整配置。