Skip to content

Instantly share code, notes, and snippets.

@wojtekadams
Last active July 11, 2025 05:52
Show Gist options
  • Select an option

  • Save wojtekadams/42a6fa54828a680f8eefcfcb1cfa66e6 to your computer and use it in GitHub Desktop.

Select an option

Save wojtekadams/42a6fa54828a680f8eefcfcb1cfa66e6 to your computer and use it in GitHub Desktop.
Proxmox - script that shuts down VMs and Containers before the hypervisor is shut down
#!/bin/bash
# get list of VMs and LXCs on the node
VMIDs=$(/usr/sbin/qm list | tail -n +2 | awk '/[0-9]/ {print $1}')
LXCs=$(/usr/bin/lxc-ls)
# ask LXCs to stop
for LXC in $LXCs
do
/usr/bin/lxc-stop $LXC
done
# ask VMIDs to shutdown
for VM in $VMIDs
do
/usr/sbin/qm shutdown $VM
done
#wait until they're done (and down)
for LXC in $LXCs
do
while [[ $(lxc-info $LXC | grep State | awk '{print $2}') =~ RUNNING ]] ; do
sleep 1
done
done
for VM in $VMIDs
do
while [[ $(/usr/sbin/qm status $VM) =~ running ]] ; do
sleep 1
done
done
# do the poweroff
/usr/sbin/poweroff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment