How to run "Oracle VirtualBox (VBOX)" like a service after boot in fully background "Microsoft Windows (WIN)"?

StackOverflow https://stackoverflow.com/questions/19017825

Question

I want to run autostart "Virtual Machine (VM)" without "Oracle VirtualBox (VBOX)" "Graphical User Interface (GUI)" on local DEV machine with "Microsoft Windows (WIN)".

How can I do that in fully running background process?

Was it helpful?

Solution

SOLUTION

The trick is to run the VM without GUI. With this you can easily run VM on WIN server like a service too.

Prerequired is that exist some VM, you have some already. Below put its name instead {vm_name}.


1) At first we use build-in executable file "VBoxHeadless.exe".

Create file

vm.run.bat

with

cd "c:\Program Files\Oracle\VirtualBox\"
VBoxHeadless.exe -s {vm_name} -v on

run and test it - with WIN "Command Line Interface (CLI)" called "Command shell" - and VM will be opened running in background.

vm.run.bat

2) Then we use "Windows-based script host (WSCRIPT)" and language "Microsoft Visual Basic Script (VBS)" and run above file "vm.run.bat".

Create file

vm.run.vbs

put code

Set WshShell = WScript.CreateObject("WScript.Shell")
obj = WshShell.Run("vm.run.bat", 0)
set WshShell = Nothing

run and test it - CLI will be run in background.

wscript.exe vm.run.vbs

Ref

OTHER TIPS

You can use VBoxManage to start a VM headless:

"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" startvm "Your VM name" --type headless

I used something similar to Samuel's solution that works great.

On the desktop (or any folder), right click and go to New->Shortcut.

In the target, type:

"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" startvm {uuid} --type headless

In the name, type whatever you want and click Finish.

Then to stop the same vm, create a new shortcut with the target being:

"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" controlvm {uuid} poweroff

Double clicking these starts and stops the VM without any window staying open.

Starting from VirtualBox 4.2 you can just hold Shift while launching the vm to run it fully in background. See details here.

The truly most-consistent option is to use Task Scheduler.

Implementing the solution

This requires a couple of pretty easy steps, but I will explain them in detail to ensure anyone from with any technical background can set this up:

  1. Identify your virtual machine name
  2. Create a task in Task Scheduler

1. Identify your virtual machine name

Navigate to C:\Users\YourUserNameHere\VirtualBox VMs

VirtualBox VMs Folder

The folder name above generally reflects the virtual machine name. You can confirm this by checking VirtualBox Manager itself:

VirtualBox GUI

The machine name is WindowsXPSP3.

2. Create a task in Task Scheduler

First click the start button and type "task scheduler" without the quotes. Then open the Task Scheduler:

Task Scheduler Search

Inside the task scheduler, we're going to see a structure tree on the left side. Right-click on Task Scheduler Library. Left-click on New Folder...:

Task Scheduler New Folder

Name the folder something memorable, like User Custom and hit OK (if you already have an existing folder that you would prefer to use, that's fine as well, skip to the next paragraph instead):

Name New Folder

Click your newly created folder, in my case User Custom, to highlight it. Right-click in the empty list to the right and Left-click on Create New Task...:

Create New Task

Now comes the tricky stuff. Follow my instructions verbatim. If you feel like downvoting because it didn't work, or say "this didn't work for me" in the comments, I'm betting you skipped a step here. Come back and try it again.

The Name and Description can be whatever you like, it is merely aesthetic and will not affect functionality. I'm going to name mine after my virtual machine and put a brief description. What IS important is that you choose Run whether user is logged on or not and Run with highest privileges:

Create Task: General

Switch to the Triggers tab at the top and Left-click New.... Switch the Begin the task: combination box to At Startup and then Left-click OK:

New Trigger

Switch to the Actions tab at the top and Left-click New.... Click browse (do not try to type this manually, you will cause yourself headaches) and navigate to C:\Program Files\Oracle\VirtualBox. Highlight VBoxManage.exe and Left-click Open:

Browse to VBoxManage

Copy everything except the executable and the quotation marks from Program/script: into Start in (optional)::

Copy Directory Path

Finally, put the following line into Add arguments (optional): and hit OK:

startvm "YourVirtualMachineNameFromStep1" --type headless

in my case, I will use:

startvm "WindowsXPSP3" --type headless

Enter Arguments

My Conditions tab is generally set to the following:

Conditions Tab

Make sure your Settings tab looks like the following, but absolutely ensure you have set the items marked in yellow to match mine. This will make sure that if some pre-requisite wasn't ready yet that it will retry a few times to start the virtual machine and that the virtual machine won't be terminated after 3 days. I would leave everything else as default unless you know what you are doing. If you don't do what I show you here, and it ends up not working, it's your problem:

Settings Tab

Finally, hit OK at the bottom of the Create Task window. You are done!

Testing the solution

Testing My Fake Scenario Above (and how you can test yours)

When I restart my computer, I can log in and open the VirtualBox Manager and see that my VM is running:

Running VM

I can also open Task Scheduler back up, and verify that it ran successfully, or see what the error was if it did not (most errors will be directory errors from people trying to manually enter where I told them not to):

Task Scheduler Success

Testing My Actual Use Case

On another machine, I set up my Linux Server as a virtual machine with it's own raw solid-state hard drive. I wanted that Server to boot back up if the machine got restarted (crash, windows update, etc) automatically, without the user having to log in. I set that one up exactly as I described above and restarted that machine. I know it worked successfully because I was able to access my Samba share (laymens: a folder with stuff in it that I share over my network to my other computers) from another computer WITHOUT having first logged into the machine that runs the Server VM. This 100% confirms that it does start on system boot and not after the user logs in.

An alternative solution : http://vboxvmservice.sourceforge.net/

It works perfect for me !

If you do not mind operating the application once manually, to end with OS running in background; here are the options:

Open Virtual Box. Right Click on your Guest OS > Choose: Start Headless. Wait for a while till the OS boots.

Then close the Virtual Box application.

There an easy manual option right in the GUI too:

Screenshot from Virtualbox 5.2

(Taken from Virtualbox 5.2)

Following Bruno Garett's Answer, in my experience: testing the vm.run.bat file fails, gives a read only error but will work fine running the VB script. Just to save anyone time.

Also to shut down headless you can use another batch script (Sam F's solution wont work with Bruno's solution):

cd "c:\Program Files\Oracle\VirtualBox\"
VBoxManage controlvm "Ubuntu Server" acpipowerbutton

Second line was obtained from here you can use whichever option you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top