Last active
July 11, 2025 05:52
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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