使用 systemd 設置定時重啟
- 使用 root 用戶登錄終端。
- 創建一個新的 systemd 服務,例如 reboot.service。
sudo touch /etc/systemd/system/reboot.service
- 使用文本編輯器(例如vi)打開該文件,并輸入以下內容:
[Unit]Description=Reboot Service[Service]Type=oneshotExecStart=/sbin/reboot[Install]WantedBy=multi-user.target
- 保存并關閉文件。
- 創建一個新的 systemd 定時器,例如 reboot.timer。
sudo touch /etc/systemd/system/reboot.timer
- 使用文本編輯器打開該文件,并輸入以下內容:
[Unit]Description=Reboot Timer[Timer]OnCalendar=*-*-* 02:30:00[Install]WantedBy=timers.target
- 注意OnCalendar后面的時間格式是 – – 02:30:00,其中第一個表示星期,第二個表示月份,第三個表示日期,后面的時間格式是小時:分鐘:秒。
- 保存并關閉文件。
- 啟用并啟動定時器。
sudo systemctl enable –now reboot.timer
這樣就可以在每天早上 2:30 重啟了。
使用 cron 設置定時重啟
- 使用 root 用戶登錄終端。
- 打開 cron 配置文件:
crontab -e
- 在文件末尾加入下面一行,表示每天凌晨 2:30 重啟:
30 2 * * * /sbin/reboot
- 注意這里的時間格式是分鐘 時 日 月 星期,依次對應上面的 30 2 *
- 保存并退出。