shutdown-all-vms 1.1

add lxc shutdown sequence. Shifted 2.5 min wait to the sleep command instead of the shutdown commands.
main
superrob1500 2025-08-31 19:33:27 +02:00
parent 7996b029f2
commit 8d2402e4b6
1 changed files with 26 additions and 5 deletions

View File

@ -1,34 +1,55 @@
#!/bin/bash #!/bin/bash
# Script to try to gracefully shut down all running VMs on a Proxmox host, before then force stopping them. # 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. # 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
# Get a list of running VMs # Get a list of running VMs
vmlist=$(qm list | grep running | awk '{print $1}') 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 # Loop through the list and force shutdown each VM
echo "Stopping VM(s)..."
for vmid in $vmlist for vmid in $vmlist
do do
echo "Shutting down VM $vmid" echo "Shutting down VM $vmid"
qm shutdown $vmid --timeout 150 # Try to force shutdown VMs with a 2.5 minute timeout qm shutdown $vmid
done
echo "Stopping container(s)..."
for lxcid in $lxclist
do
echo "Shutting down container $lxcid"
pct shutdown $lxcid
done done
# Wait for 30 seconds to allow VMs to shut down # Wait for 30 seconds to allow VMs to shut down
sleep 30 echo "/nWating for shutdowns to complete."
sleep 150
# Get a list of running VMs again # Get a list of running VMs again
vmlist=$(qm list | grep running | awk '{print $1}') 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 # If there are still running VMs, print a warning and kill them
if [ -n "$vmlist" ]; then if [ -n "$vmlist" ]; then
echo "WARNING: The following VMs are still running:" echo "WARNING: The following VM/container(s) are still running:"
echo $vmlist echo $vmlist
echo $lxclist
echo "Will attempt to kill them now" echo "Will attempt to kill them now"
for vmid in $vmlist for vmid in $vmlist
do do
echo "Killing VM $vmid" echo "Killing VM $vmid"
qm stop $vmid qm stop $vmid
done done
for lxcid in $lxclist
do
echo "Killing container $lxcid"
qm stop $lxcid
done
fi fi
# Exit with success # Exit with success