Skip to content

Instantly share code, notes, and snippets.

@kennith
Last active May 30, 2019 19:40
Show Gist options
  • Save kennith/9375d90d918cc408bf4f721c161e06ee to your computer and use it in GitHub Desktop.
Save kennith/9375d90d918cc408bf4f721c161e06ee to your computer and use it in GitHub Desktop.
function vm-shutdown() {
echo 'Shutdown running VM(s)';
# Get a list of VMs that are running and put them into array
vms=($(/usr/local/bin/VBoxManage list runningvms | awk '{print $1}' | tr -d '"'))
for vm in "${vms[@]}"
do
echo "Shutting down $vm ";
/usr/local/bin/VBoxManage controlvm $vm acpipowerbutton
done
runningvms=$(/usr/local/bin/VBoxManage list runningvms | wc -l | xargs)
# Visual feedback while waiting for the VMs to shutdown
while [ $runningvms -gt 0 ]
do
runningvms=$(/usr/local/bin/VBoxManage list runningvms | wc -l | xargs)
echo "Shutting down $runningvms VM(s)..."
sleep 2
done
echo 'VM shutdown complete';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment