目前的电脑主板都有定时开机的功能,最简单的是设置为每天都定时开机,然后配合系统的定时关机功能,实现自动的开、关机工作。 这里主要是为我的公司的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