云服务器Linux设置定时重启
发布时间:2024-11-21 09:34
阅读量:1028
携手合作伙伴,实现业务上的双向合作共赢
我们为您提供全方位的支持与服务,确保您在使用我们的云服务时无忧无虑。
10+年商誉沉淀,深耕中国香港及海外高端资源
Linux定时重启
本文介绍如何设置定时重启Linux主机
设置方法
说明
下面以Ubuntu的操作方式为例(CentOS的操作方式完全一样)
在 Ubuntu 系统中,有两种方法可以实现定时重启,分别是使用 systemd 和 cron。
使用 systemd 设置定时重启
1. 使用 root 用户登录终端。
2. 创建一个新的 systemd 服务,例如 reboot.service。
sudo touch /etc/systemd/system/reboot.service
1. 使用文本编辑器(例如vi)打开该文件,并输入以下内容:
[Unit]
Description=Reboot Service
[Service]
Type=oneshot
ExecStart=/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
sudo systemctl enable --now reboot.timer
这样就可以在每天早上 2:30 重启了。
使用 cron 设置定时重启
1. 使用 root 用户登录终端。
2. 打开 cron 配置文件:
crontab -e
1. 在文件末尾加入下面一行,表示每天凌晨 2:30 重启:
30 2 * * * /sbin/reboot
这样cron就会在每天早上2:30重启系统了。
总结一下,使用systemd和cron都可以实现在 Ubuntu 系统中定时重启的功能,两种方法都需要使用root权限,并且重启命令都是reboot。只是在时间格式上有所不同,具体可以参考上面的操作步骤。
服务热线:
18251482443电子邮箱:
202256327@qq.com商务QQ:
202256327公司地址:
江苏省连云港市东海县红星美凯龙S5-A133
客服微信
客服微信