Skip to content

Instantly share code, notes, and snippets.

@keuv-grvl
Created August 17, 2018 00:12
Show Gist options
  • Save keuv-grvl/dac1d7c8edb80b39a8577222508c292b to your computer and use it in GitHub Desktop.
Save keuv-grvl/dac1d7c8edb80b39a8577222508c292b to your computer and use it in GitHub Desktop.
Run a Virtualbox VM in background and safely save its state if SIGINT is sent
#!/usr/bin/env bash
# trap signals
trap vmsavestate INT
function vmsavestate() {
cleaner=$(head -c $(( $(tput cols) - 1 )) < /dev/zero | tr '\0' ' ')
echo -en "\r${cleaner}\rSaving state of 'win7word' "
vboxmanage controlvm win7word savestate
}
# run the VM and wait for normal exit or Ctrl-C
/usr/lib/virtualbox/VirtualBox --comment "win7word" --startvm "0c2c6c8f-c448-448c-8a53-683f56439613" &
PID=$!
echo -n "'win7word' virtual machine is running under pid $PID. You may safely terminate the VM using Ctrl-C."
wait $PID # wait for SIGINT or normal exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment