#!/bin/bash # Script to try to gracefully shut down all running VMs on a Proxmox host, before then force stopping them. # Running as a sub-script of apcupsd's doshutdown function to ensure that VMs are shut down before the host is powered off. #v1.1.2 # Get a list of running VMs vmlist=$(qm list | grep running | awk '{print $1}') lxclist=$(pct list | grep running | awk '{print $1}') echo "Currently running instances" echo "VM(s) $vmlist" echo "Container(s) $lxclist" # Loop through the list and force shutdown each VM echo "Shutting down VM(s)..." for vmid in $vmlist do echo "Sending shutown command to VM $vmid" qm shutdown $vmid & sleep 2 done echo "Shutting down container(s)..." for lxcid in $lxclist do echo "Sending shutown command to container $lxcid" pct shutdown $lxcid & sleep 2 done # Wait for 2.5 minutes to allow VMs to shut down echo -e "\nWating 2.5 minutes for shutdowns to complete." sleep 150 # Get a list of running VMs again vmlist=$(qm list | grep running | awk '{print $1}') lxclist=$(pct list | grep running | awk '{print $1}') # If there are still running VMs, print a warning and kill them if [[ -n "$vmlist" || -n "$lxclist" ]]; then echo "WARNING: The following VM/container(s) are still running:" echo $vmlist echo $lxclist echo "Will attempt to kill them now" for vmid in $vmlist do echo "Killing VM $vmid" qm unlock $vmid qm stop $vmid & done for lxcid in $lxclist do echo "Killing container $lxcid" pct unlock $lxcid pct stop $lxcid & done fi # Exit with success echo -e "\nShutdowns complete." exit 0