PVE Host

Table of Contents

开启自动睡眠

vim /etc/systemd/logind.conf

如下设置将开启空闲超过30分钟后自动睡眠。

IdleAction=suspend
IdleActionSec=30min

Trigger new settings

systemctl restart systemd-logind

当guest运行时,禁用休眠功能

后台启动如下进程即可:

  1. 检测代码
#!/usr/bin/env bash

vmid=100

while true; do
        active_count=$(/usr/sbin/qm satus 100 | grep running | wc -l)
        if [[ $active_count -gt 0 ]]; then
                # inhibit host sleep
                systemd-inhibit --what=sleep \
                                --who="Proxmox Guest" \
                                --why="Guest VM is running" \
                                sleep 30
        fi
        # wait for 1 second before checking againe
        sleep 1
done
  1. systemd service

    [Unit]
    Description=PVE guest status monitor
    After=network.target
    
    [Service]
    ExecStart=/usr/local/bin/pve-guest-monitor.sh
    ExecReload=/bin/kill -HUP $MAINPID
    KillMode=process
    Restart=on-failure
    RestartSec=42s
    
    [Install]
    WantedBy=multi-user.target
    

一键安装脚本

the whole script can be found at: assets/.pve-prevent-host-sleep.sh

Contact me via :)
虚怀乃若谷,水深则流缓。