云服务器Linux设置定时重启

技术问题

云服务器Linux设置定时重启

2024-06-19 09:54


云服务器Linux设置定时重启

                                            




使用 systemd 设置定时重启

  1. 使用 root 用户登录终端。
  2. 创建一个新的 systemd 服务,例如 reboot.service。

sudo touch /etc/systemd/system/reboot.service

  1. 使用文本编辑器(例如vi)打开该文件,并输入以下内容:

[Unit]Description=Reboot Service[Service]Type=oneshotExecStart=/sbin/reboot[Install]WantedBy=multi-user.target

  1. 保存并关闭文件。
  2. 创建一个新的 systemd 定时器,例如 reboot.timer。

sudo touch /etc/systemd/system/reboot.timer

  1. 使用文本编辑器打开该文件,并输入以下内容:

[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 设置定时重启

  1. 使用 root 用户登录终端。
  2. 打开 cron 配置文件:

crontab -e

  1. 在文件末尾加入下面一行,表示每天凌晨 2:30 重启:

30 2 * * * /sbin/reboot

  • 注意这里的时间格式是分钟 时 日 月 星期,依次对应上面的 30 2 *
  • 保存并退出。

标签:
  • 云服务器Linux设置定时重启