If the vboxd scripts are giving you as much trouble as they did me, you might want to try this; it's a rather simpler solution, implemented in Perl, which provides the same functionality. Specifically, the VM starts up with the system (assuming the service is set to start automatically, which is the default), and goes down cleanly when the service is stopped.
Major differences from the vboxd solution, aside from being considerably simpler to deal with in my experience, are that this script doesn't pass a VRDE port setting to VBoxHeadless, and that the VM shutdown is handled via "acpipowerbutton" rather than "savestate".
If this doesn't suit your purposes, it's easy enough to change; in the first case, add '-e "TCP/Ports=[,port...]"' to the VBoxHeadless command line string ($cmd), and in the second case, just change "acpipowerbutton" to "savestate" in the SIGTERM handler.
Here's the Perl script:
#!/usr/bin/perl
# Service wrapper for VirtualBox VM.
use strict;
# Windows-style path pointing to your VirtualBox home directory
# (i.e., where your VirtualBox.xml lives)
my $vboxhome = 'c:\\Documents and Settings\\Aaron\\.VirtualBox';
# Cygwin path pointing to the VirtualBox install directory
my $vboxpath = '/cygdrive/c/Program Files/Oracle/VirtualBox';
# Name or UUID of your virtual machine
my $vmname = '{83dfb4b1-4243-4a07-9d37-9df5573792d8}';
$ENV{'VBOX_USER_HOME'} = $vboxhome;
my $exit_handler = sub {
print "Caught SIGTERM; shutting down VM.\n";
exec("'$vboxpath/VBoxManage.exe' controlvm $vmname acpipowerbutton");
};
$SIG{TERM} = $exit_handler;
print "[debug] pid $$\n";
# Command line for VBoxHeadless - add your VRDE port here if you want one
my $cmd = "'$vboxpath/VBoxHeadless.exe' --startvm $vmname 2>&1 1>/dev/null";
print "[debug] $cmd\n";
system($cmd);
print "VM died unexpectedly; exiting.\n";
And here's the cygrunsrv command line I used to install the Windows service:
cygrunsrv --install '<service-name>' --desc '<description>' \
--path '/usr/bin/perl' --args '<full-path-to-service-script>' \
--chdir '<path-to-service-script-dir>' --termsig TERM --shutsig TERM \
--preshutdown --interactive
From there, it should just be a matter of issuing 'sc start <service-name>', and you should be good to go -- remember that, by default, cygrunsrv services log to /var/log/<service-name>.log, so if anything misbehaves, that's the first place to look for a reason why.
One thing to keep in mind is that, when you run the VM this way, it will run under the built-in SYSTEM account, rather than your own. This being the case, you won't be able to administer the VM via the VirtualBox console while it's running as a service; it will appear to be in the "Powered off" state, and you won't be able to get to its settings. (Trying to power on the VM while it's running as a service is not recommended -- I don't know what will happen, but at best it'll be a no-op, and at worst it'll hose your VM.) If you need to change the VM's settings or access the console, stop the service first (and wait for VBoxHeadless.exe to exit), and then configure it/power it up in the VirtualBox console as you normally would.
You could probably solve this problem by installing a second interactive service to run the VirtualBox console; since that would run the console under the SYSTEM account as well, it'd most likely be able to see and manage the running VM. I haven't had a need to do that myself, so I'm not sure it would work, but I don't see a reason why it wouldn't. (If you try it, I'd appreciate a comment to this answer, letting me know how it worked out for you. Thanks!)
Hope this helps!
The answer has nothing to do with running as a service. The startup happen after a user logon, completely different that the services behavior. – Felice Pollano – 2016-01-25T10:10:26.827
13This is not a service. This only runs VBox when you log in - not when the machine is powered on. VBoxHeadless is the way to go for running as a service. See my answer below (which I'm about to improve per Aaron Copley's comment). – John Hart – 2012-04-10T17:41:59.437
It appears to works fine, Is there anyway to "hide" the window for the virtual machine also, so it be running more like a windows service? – Jhonny D. Cano -Leftware- – 2010-01-05T19:41:46.973
running VBox as a service will not hide the window. you can minimize it automatically to the system tray with AWM though: http://www.eusing.com/WindowManager/WindowManager.htm
– None – 2010-01-05T19:59:13.91315checkout "VBoxHeadless" for launching VM's with no UI. You will need to use RDP if you want to see the VM's screen. – Mark Porter – 2010-01-05T20:25:02.300