Skip to content

Instantly share code, notes, and snippets.

@zulfadlisukarno
Created December 14, 2020 15:20
Show Gist options
  • Save zulfadlisukarno/5a03e64e1d6e030cf171a1005cf2916e to your computer and use it in GitHub Desktop.
Save zulfadlisukarno/5a03e64e1d6e030cf171a1005cf2916e to your computer and use it in GitHub Desktop.
This script will graceful power down all VM and wait for 30 seconds. After that it will kill remaining VM that still not powerdown.
#!/bin/bash
row=`vboxmanage list runningvms | wc -l`
col=0
# Send ACPI shutdown first
while read -r name; do
echo "Stopping VM $(echo $name |cut -d \{ -f 1)..."
VBoxManage controlvm "$(echo $name |cut -d \{ -f 2 |sed 's/.$//')" acpipowerbutton &> /dev/null
done < <(VBoxManage list runningvms)
msg="wait for${1}... "
#clear
tput cup $row $col
echo -n "$msg"
l=${#msg}
l=$(( l+$col ))
for i in {30..1}
do
tput cup $row $l
echo -n "$i seconds"
sleep 1
done
list_runningvms=`vboxmanage list runningvms | wc -l`
echo
if [ $list_runningvms -ne 0 ];
then
# Read all pids for "VBoxHeadless" process, and kill (-9) them.
echo "Not all VM is poweroff. Force killing.."
while read -r pid; do
echo "Killing VirtualBox Process '${pid}'..."
kill -9 ${pid}
echo " OK Done."
done < <(pgrep VBoxHeadless)
else
echo "All VM gracefully poweroff. no need force killing."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment