From 7996b029f2d5cb806ce578b0e7e009ea629349a7 Mon Sep 17 00:00:00 2001 From: superrob1500 Date: Sun, 31 Aug 2025 18:55:52 +0200 Subject: [PATCH] shutdown-all-vms.sh init --- shutdown-all-vms.sh | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 shutdown-all-vms.sh diff --git a/shutdown-all-vms.sh b/shutdown-all-vms.sh new file mode 100644 index 0000000..63f8b33 --- /dev/null +++ b/shutdown-all-vms.sh @@ -0,0 +1,35 @@ +#!/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. + +# Get a list of running VMs +vmlist=$(qm list | grep running | awk '{print $1}') + +# Loop through the list and force shutdown each VM +for vmid in $vmlist +do + echo "Shutting down VM $vmid" + qm shutdown $vmid --timeout 150 # Try to force shutdown VMs with a 2.5 minute timeout +done + +# Wait for 30 seconds to allow VMs to shut down +sleep 30 + +# Get a list of running VMs again +vmlist=$(qm list | grep running | awk '{print $1}') + +# If there are still running VMs, print a warning +if [ -n "$vmlist" ]; then + echo "WARNING: The following VMs are still running:" + echo $vmlist + echo "Will attempt to kill them now" + for vmid in $vmlist + do + echo "Killing VM $vmid" + qm stop $vmid + done +fi + +# Exit with success +exit 0 \ No newline at end of file