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