最近搞了几个SAS盘,晚上睡觉闹得慌,给他们弄个自动关机服务
创建关机脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
vim /usr/local/bin/pve-scheduled-shutdown.sh
# 写入如下内容
#!/bin/bash
echo "$(date): Stopping all VMs..." >> /var/log/pve-scheduled-shutdown.log
# 关闭所有正在运行的虚拟机
for vmid in $(qm list | awk '$3 == "running" {print $1}'); do
qm shutdown $vmid &
done
# 等待关机完成
sleep 300
echo "$(date): Shutting down host..." >> /var/log/pve-scheduled-shutdown.log
/sbin/shutdown -h now
|
设置可执行权限
1
|
chmod +x /usr/local/bin/pve-scheduled-shutdown.sh
|
创建systemd服务文件
1
2
3
4
5
6
7
8
9
10
|
vim /etc/systemd/system/pve-scheduled-shutdown.service
# 内容
[Unit]
Description=PVE Scheduled Shutdown
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/pve-scheduled-shutdown.sh
|
创建定时器
1
2
3
4
5
6
7
8
9
10
11
12
|
vim /etc/systemd/system/pve-scheduled-shutdown.timer
# 内容 eg.每天23:30定时关机
[Unit]
Description=Run scheduled shutdown daily at 23:30
[Timer]
OnCalendar=*-*-* 23:30:00
Persistent=true
[Install]
WantedBy=timers.target
|
启动定时器
1
2
3
4
5
6
|
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable --now pve-scheduled-shutdown.timer
# 查看定时器
systemctl list-timers --all | grep pve-scheduled-shutdown
|