Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

My client needs to have a virtualized Linux running on his Windows Server 2008 (R1). I've managed to setup VirtualBox as a service with srvany, but I see no way of creating a kind of 'stop script' so the virtual machine powers off correctly when I stop the registered service.

I know there may be other options of creating a headless VM, but that's not the point. How can I create a 'stop script' that is executed when I stop the registered service (or the system is rebooted)?

Or maybe, there are some other ways to launch 'system shutdown' scripts so VM is stopped properly on shutdown?


(for those that might be interested)

How I created a VirtualBox service

  1. Setup a virtual machine
  2. Install "Windows Server 2003 Resource Kit Tools.exe".
  3. Copy ".VirtualBox" folder to 'C:\Windows\System32\config\systemprofile\' so 'system' user (which runs services) has its VirtualBox machines there.
    WARN: You'll need to correct VirtualBox.xml paths to snapshots, if your VM has any.
  4. Create an empty service named "UbuntuD" with the help of srvany:
    sc create UbuntuD binPath= "C:\Program Files (x86)\Windows Resource Kits\Tools\srvany.exe" start= auto DisplayName= "Ubuntu Daemon"
  5. Specify the service start command. Add these keys to registry
    [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\UbuntuD\Parameters]
    Application="C:\Program Files\Sun\VirtualBox\VBoxHeadless.exe"
    AppParameters="--vrdp=off -s Ubuntu"
    AppDirectory="C:\Program Files\Sun\VirtualBox\"
  6. There's no 'stop script' for our hacky service, so we add a shutdown script in 'Local Policy' -> Computer configuration -> Windows Settings -> Scripts (Startup/shutdown):
    C:\Program Files\Sun\VirtualBox\vboxmanage, params: controlvm Ubuntu savestate
  7. Run the service
share|improve this question

2 Answers 2

up vote 3 down vote accepted

"Real" service programs have code to handle performing cleanup and shutdown activities. You're running a program that wasn't intended to be run as a service, and it has no such code to handle graceful termination when the Service Control Manager (SCM) signals that the service is stopping.

Somebody could write a nice service wrapper for VirtualBox that could gracefully handle running as a service. That's the real solution. Unfortunately, you're not going to come up with a "hack" that will do what you're looking for. SRVANY (and every other program of the same type that I'm aware of) doesn't have any capability to execute user-specified code when SCM signals that the service is stopping.

share|improve this answer
    
Thank you, I've suspected it's not possible without hacking. But maybe, there are system-wide 'shutdown' scripts executed when Windows is shutting down? If so, it can be a rather nice replacement :) –  kolypto Nov 25 '09 at 19:47
1  
There are "Shutdown scripts" in Windows. These were added in Windows 2000 (and remain in every version since) and can be assigned either through local policy or Active Directory group policy. I'm not familiar with VirtualBox and I'm not aware if you can use an IPC mechaism to signal to the running VirtualBox process that it should shut down cleanly. –  Evan Anderson Nov 25 '09 at 20:00
    
That's it, thanks! I knew how to stop it properly, but didn't know where to store this command. This way of launching services looks really ugly after using 'init.d' in Linux! :) –  kolypto Nov 25 '09 at 20:08

http://answers.yahoo.com/question/index?qid=20071011212557AAofTy6

Create a VBS file (i.e., a VB Script). Open Notepad, and type the following code in it:

'=====================================
Set WshShell = WScript.CreateObject("WScript.Shell")
obj = WshShell.Run("H:\test.bat", 0)
set WshShell = Nothing
'=====================================

Replace the "H:\test.bat" in the above code with the full path of your own BAT file, but do not forget the double quotation marks around the BAT file's full path.

SAVE the file. Give it a name like HIDECMDWINDOW.VBS. The extension has to be VBS.

Double click the VBS file to run it. Your BAT file should run without showing the command window.

If your BAT file takes arguments, you can pass arguments to it too - the script for that will be a little more advanced, but if you need it, leave me a message.

Credit for the above goes to Koushik Biswas as original poster.

Posted here because this was the link that came up most for me in my search and what i've linked to is probably what most people who find this are looking for.

share|improve this answer
    
This does not answer the question at all. This is how to run a command file in the background. It doesn't matter if you think that "most people" want to find it here. This answer should be downvoted below 0. –  Jeremy Feb 16 '12 at 13:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.