From 0f9ac29b7505f8ec344a99a03adcd783265105bc Mon Sep 17 00:00:00 2001 From: superrob1500 Date: Mon, 1 Sep 2025 02:05:39 +0200 Subject: [PATCH] shutdown-all-vms - 1.1.1 Shutdown commands now are sent to the back to shutdown VMs and containers in as parallel a fashion as possible. Addition of function to unlock VMs and Containers when attempting to kill them. Add check for hung containers to kill. Rephrasing of output. --- shutdown-all-vms.sh | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/shutdown-all-vms.sh b/shutdown-all-vms.sh index 5bb2d9c..d439f43 100644 --- a/shutdown-all-vms.sh +++ b/shutdown-all-vms.sh @@ -1,7 +1,7 @@ #!/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 +#v1.1.1 # Get a list of running VMs vmlist=$(qm list | grep running | awk '{print $1}') @@ -9,25 +9,25 @@ lxclist=$(pct list | grep running | awk '{print $1}') echo "Currently running instances" echo "VM(s) $vmlist" -echo "Container(s)$lxclist" +echo "Container(s) $lxclist" # Loop through the list and force shutdown each VM echo "Stopping VM(s)..." for vmid in $vmlist do - echo "Shutting down VM $vmid" - qm shutdown $vmid + echo "Sending shutown command to VM $vmid" + qm shutdown $vmid & done echo "Stopping container(s)..." -for lxcid in $lxclist +for lxcid in $lxclist do - echo "Shutting down container $lxcid" - pct shutdown $lxcid + echo "Sending shutown command to container $lxcid" + pct shutdown $lxcid & done -# Wait for 30 seconds to allow VMs to shut down -echo "/nWating for shutdowns to complete." +# Wait for 2.5 minutes to allow VMs to shut down +echo -e "\nWating for shutdowns to complete." sleep 150 # Get a list of running VMs again @@ -35,7 +35,7 @@ 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" ]; then +if [[ -n "$vmlist" || -n "$lxclist" ]]; then echo "WARNING: The following VM/container(s) are still running:" echo $vmlist echo $lxclist @@ -43,12 +43,14 @@ if [ -n "$vmlist" ]; then for vmid in $vmlist do echo "Killing VM $vmid" - qm stop $vmid + qm unlock $vmid + qm stop $vmid & done for lxcid in $lxclist do echo "Killing container $lxcid" - qm stop $lxcid + pct unlock $lxcid + pct stop $lxcid & done fi