CentOS8 的定时关机配置方法,使用systemd实现

目前的电脑主板都有定时开机的功能,最简单的是设置为每天都定时开机,然后配合系统的定时关机功能,实现自动的开、关机工作。 这里主要是为我的公司的samba服务器使用,让它每天8:30开机,工作日晚上21:00关机,周六、周日则在8:40关机。

1.编写poweroff.sh 脚本

vi /etc/systemd/system/poweroff.sh
#内容仅1个单词:
poweroff

2.编写shutdown.service

vi /etc/systemd/system/shutdown.service
#内容:
[Unit]
Description = poweroff the local machine

[Service]
ExecStart =/bin/bash /etc/systemd/system/poweroff.sh
Restart = no
Type = simple

[Install]
WantedBy = multi-user.target

3. 为poweroff.sh和shutdown.service 增加可执行权限

chmod +x /etc/systemd/system/poweroff.sh
chmod +x /etc/systemd/system/shutdown.service

4.编辑everyday.timer和 holiday.timer两个定时器

#设定工作日的晚间关机时间
vi /etc/systemd/system/everyday.timer

[Unit]
Description=Poweroff every work day

[Timer]
# Power off in working days at 21:00
OnCalendar=Mon,Tue,Wed,Thu,Fri *-*-* 21:00:00
# Call necessary service
Unit=shutdown.service

[Install]
WantedBy=timers.target

#设定周六、周日的关机时间
vi /etc/systemd/system/holiday.timer

[Unit]
Description=Poweroff at holiday

[Timer]
# Power off in holidays at 08:40
OnCalendar=Sat,Sun *-*-* 08:40:00
# Call necessary service
Unit=shutdown.service

[Install]
WantedBy=timers.target

5. 将everyday.timer和 holiday.timer两个定时器设定为开机自启动

systemctl enable --now {everyday.timer,holiday.timer}

6.显示目前系统中正在运行的定时器

systemctl list-timers

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值