50

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

How can I do that in fully running background process?

Bruno
  • 6,623
  • 5
  • 41
  • 47
  • 1
    VBoxHeadless -s "MyGuest" & [in linux][1] [1]: http://askubuntu.com/questions/221195/how-can-i-send-vboxheadless-to-the-background-so-i-can-close-the-terminal# – asraful009 May 11 '14 at 09:38
  • @asraful009 yes sure I use it in linux but in windows works differently. – Bruno May 12 '14 at 12:24
  • You may want to change your accepted answer to https://stackoverflow.com/a/24200814/160665 which is the way it is intended by VirtualBox and does not require the indirections via batch and VBS files (hence less error-prone). – exhuma Jul 21 '19 at 14:01
  • @exhuma thx for your feedback - I have no tested today if that command can be properly used for "windows service" – Bruno Jul 22 '19 at 09:52
  • @BGBruno good point. I've only tried it as windows startup via shortcut. I will try it in the coming days as service and will let you know. – exhuma Jul 22 '19 at 11:58
  • in 2013 I publicly hosted #virtualbox so I have that command too - I think by "sc.exe" - feel free share it @exhuma – Bruno Jul 22 '19 at 20:04

9 Answers9

49

How can I run VBOX under WIN like a service with script?

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) Create script BAT

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) Create script VBS

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bruno
  • 6,623
  • 5
  • 41
  • 47
  • 1
    Is there a way to save the machine state after aka hibernate it? – Daniil Shevelev Feb 11 '15 at 22:46
  • It seems like this is a bit long winded but its definitely the way to go - I tried to setup a scheduled task just on the raw program but it fails with a mysterious error code if you try and run it as system or as a different session. – JonnyRaa Feb 23 '15 at 10:30
  • 1
    @DaSh Yes, use the command-line based utility "VBoxManage.exe" instead. Create a shortcut such as the following: `"C:\Program Files\VirtualBox\VBoxManage.exe" controlvm "{vmname}" savestate` (bada-bing bada-boom!) – Chiramisu Jun 02 '15 at 08:14
  • There is now a [much better solution](https://stackoverflow.com/a/24200814/160665) – exhuma Jul 21 '19 at 13:58
  • 1
    The *"How to ...?* form is broken English. Either use [a real](https://www.youtube.com/watch?v=t4yWEt0OSpg&t=1m49s) [English question](https://www.youtube.com/watch?v=kS5NfSzXfrI) or drop the question mark. – Peter Mortensen Jan 17 '23 at 20:49
  • See also *[Is it ever correct to have a space before a question or exclamation mark?](https://english.stackexchange.com/questions/4645/is-it-ever-correct-to-have-a-space-before-a-question-or-exclamation-mark)*. – Peter Mortensen Jan 17 '23 at 20:50
  • thx @PeterMortensen I appreciate your guidance - I just write this way for better clarity , because by me " question mark is not part of word " – Bruno Jan 18 '23 at 21:15
43

You can use VBoxManage to start a VM headless:

"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" startvm "Your VM name" --type headless
Samuel
  • 625
  • 6
  • 4
  • You don't need an additional VBScript. You can do send command in background directly from the batch file: start "MyBackgroundVM" "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" startvm {uuid} --type headless – Roman Haefeli Apr 24 '17 at 06:58
19

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

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

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sam F
  • 248
  • 2
  • 7
16

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 prerequisite 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 three 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 its 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.

DeeJayh
  • 333
  • 3
  • 9
  • You can simply change the `Begin the task:` to `At logon` instead of `At Startup` and `Run only when user is logged on` instead of `Run whether the user is logged on or not`, if you desire it to occur ONLY AFTER the user logs into the system. – DeeJayh Apr 28 '21 at 18:17
  • 1
    @BGBRUNO My pleasure. Your question asked "How to run Oracle VirtualBox (VBOX) **like a service _after boot_** in fully background Microsoft Windows (WIN)?". I interpreted this as "After the computer restarts, BEFORE the user logs in". My way is the only way to do that without downloading a third-party wrapper like VBoxVMService, which is not only another potential point of failure, but prevents you from updating VirtualBox until they update the wrapper (or else the wrapper stops working, usually) Please don't forget to upvote the answer if it's useful. Thanks Bruno! – DeeJayh May 03 '21 at 16:22
  • completely agree "BEFORE the user logs in" - I noticed it just this week that this info I didn't share "command to register new service" - today I work only with #linux so I cannot properly test and update this article about script with "sc.exe" or "installutil.exe" - so cool that you shared your click window solution, so I upvoted it right away. – Bruno May 05 '21 at 14:50
  • the only problem I had after doing this is not being able to access the oracle vm when I login into the system! the vm is working but there is no way for me to access it. – Fahad Alkamli Aug 20 '23 at 14:02
12

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

mt_serg
  • 7,487
  • 4
  • 29
  • 45
8

An alternative solution: vboxsvc - VirtualBox SMF service wrapper

It works perfect for me!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eduardo Fabricio
  • 2,151
  • 2
  • 25
  • 32
  • 3
    This is the best answer since the others will kill the VM if you shutdown Windows and forgot to shut the VM down manually first. This service application lets the VM either do ACPI shutdown or save state. – stackexchanger Jun 27 '16 at 18:40
  • @stackexchanger Virtual Box , Version 5.1.14 has a option for starting in "Headless Mode" .. it seems work nice too. .and is a native feature now :) . However, I didnt know about shutdown issue.. =P Tx. – Eduardo Fabricio Apr 13 '17 at 11:42
2

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.

Bimal Poudel
  • 1,214
  • 2
  • 18
  • 41
1

There is an easy manual option right in the GUI too:

Screenshot from VirtualBox 5.2

(Taken from VirtualBox 5.2)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bernie
  • 9,820
  • 5
  • 62
  • 92
  • 1
    This option doesn't run as a background service, so it doesn't achieve what the OP refers to. There are way too many responses with this incorrect answer to this question around the net. – jgibbs Feb 18 '22 at 20:27
0

Following Bruno Garett's answer, in my experience: testing the vm.run.bat file fails. It gives a read-only error, but it will work fine running VBScript.

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

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

The second line was obtained from here. You can use whichever option you want.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DonD
  • 339
  • 3
  • 17