centos7创建定时重启任务

· 默认分类

创建服务单元文件:

创建一个新的服务单元文件,例如/etc/systemd/system/reboot.service:

[Unit]
Description=Reboot System Daily

[Service]
Type=oneshot
ExecStart=/sbin/shutdown -r now

创建定时器单元文件:

创建一个新的定时器单元文件,例如/etc/systemd/system/reboot.timer:

[Unit]
Description=Reboot System Daily Timer

[Timer]
OnCalendar=--* 01:00:00
Persistent=true

[Install]
WantedBy=timers.target

启用并启动定时器:

重新加载systemd管理器配置,启用并启动定时器:

systemctl daemon-reload
systemctl enable reboot.timer
systemctl start reboot.timer

通过以上任一方法,你都可以在CentOS系统中设置自动重启。如果你使用的是较新的系统版本,推荐使用systemd定时器的方法,因为它更现代且易于管理。如果你使用的是旧版本的系统(CentOS 6或更早),则可能需要使用cron任务。