I've been trying for hours and hours to figure out a way of running a VirtualBox machine when my system boots using upstart. I've been able to get it to boot successfully, but I can't seem to make upstart wait for it to close properly. Instead the Virtual Machine just gets killed, and loses all its active state. The guest is configured to immediately shutdown when it receives the acpishutdown signal from VBoxManage.
This is my upstart script:
I had tried it without my attempt at the loop, and obviously that didn't work. I also tried putting all of the pre-stop script in an external script - and that partly worked, although then upstart would hang indefinitely and I also could not re-start the Virtual Machine without first rebooting the host. So that approach went from no delay, to an infinite delay.
#Start Transmission's Virtual Machine on boot
description "Transmission Virtual Machine"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel 
respawn limit 5 10
kill timeout 20
exec sudo -u sean VBoxManage controlvm "Transmission Server" acpipowerbutton
while [ "$inactive" == "0" ]; do
inactive=`vboxmanage showvminfo "Transmission Server" | grep -E "No active facilities." | wc -l`
exec sudo -u sean VBoxHeadless -startvm "Transmission Server"
This must be possible to do! How can I just tell upstart to wait for 20 seconds for the guest to turn off?