Reputation: 3918
I've been having some trouble with a recent CMD batch file I wrote. This is supposed to launch my development environment at work, and for the most part it works:
@echo off
start cmd.exe
start notepad++.exe
start sublime_text.exe
start outlook.exe
start communicator.exe
start "* Starting VirtualBox ..." virtualbox.exe
start sh.exe
start firefox.exe
The challenge I'm facing is that I want to start git-bash, (sh.exe) but in the corporate config virtualbox must be up and running for vagrant, a shell-based virtualbox manager to load properly. So, because of this dependency, I need virtualbox.exe to launch and completely finish loading (we don't need to "start" and boxes) before then launching git-bash shell (sh.exe).
I've searched but fallen short on how to do this. I keep getting results recommending:
The problem with these options is the /wait won't move to the next command until VirtualBox is closed -- that's not what I want. The second option is a time-based wait which also does not solve my problem in this case.
What am I doing wrong?
Upvotes: 6
Views: 7132
Reputation: 3335
LoggedInUsers
does no longer seem to work with wait
(VirtualBox 5.2.22), get
does work, though. So I've switched to another property :
VBoxManage guestproperty wait <host> "/VirtualBox/GuestInfo/OS/NoLoggedInUsers"
Since we are only waiting for this property to come available, you could probably use any of the guestproperties under /VirtualBox/GuestInfo/OS
which are not available for a non-running machine. You can find out which they are by
vboxmanage guestproperty enumerate <host> | grep GuestInfo/OS
where <host>
should be a running. Then compare that list to the one for a non-running machine.
Upvotes: 0
Reputation: 941
Wow! Thanks paulm for inspiration.
Found one great opportunity!
I was browsing build-it guest OS properties with command :
VBoxControl guestproperty enumerate -patterns *
and I've found a nice property "/VirtualBox/GuestInfo/OS/LoggedInUsers"
During guest OS boot this property changes from "No value set!" through value "0" to "1" (if user logs in automatically). Works fine on both Windows and linux.
>VBoxManage guestproperty get "WIN7_32" "/VirtualBox/GuestInfo/OS/LoggedInUsers"
No value set!
>VBoxManage guestproperty get "WIN7_32" "/VirtualBox/GuestInfo/OS/LoggedInUsers"
Value: 0
>VBoxManage guestproperty get "WIN7_32" "/VirtualBox/GuestInfo/OS/LoggedInUsers"
Value: 1
So you don't need to add anything to Start-up on your guest OS! Sweet!
Also you can even use wait :
VBoxManage guestproperty wait "WIN7_32" "/VirtualBox/GuestInfo/OS/LoggedInUsers"
but keep in mind, that this command will return 0 once guest OS reaches Welcome screen
Upvotes: 7
Reputation: 5892
The "best" way to do this is by using VBoxManage on the host machine combined with VBoxControl on the guest machine using properties.
The way this works is that the host machine will wait for a property to be set, and the guest machine will set this property when ever you consider the machine to have "loaded". For me I simply set the property from a logon script.
So on my Linux host I say:
VBoxManage guestproperty wait My_Virtual_Machine_Name Wait_For_Logon_Event
This will wait/block forever, you can add a timeout if you wish.
Then on the Windows Guest in a logon script or machine start script I execute:
VBoxControl guestproperty set Wait_For_Logon_Event Event_Now_Set
This then causes the execution to continue on the host side:
Name: Wait_For_Logon_Event, value: Event_Now_Set, flags:
Upvotes: 3
Reputation: 24370
As you have observed, the VirtualBox/VBoxManage process will return and exit once the VM has actually turned on. There isn't a good way to hook into the guest at start, though one thing you can do is instruct VirtualBox to run a command and check for the exit code. I would insert this code in your batch file script right after starting the guest VM but before you want to start sh.exe
:
:CheckVBox
VBoxManage guestcontrol Win8 exec --image c:\Windows\System32\ipconfig.exe --wait-exit --username Goyuix --password VirtualBoxRox
IF NOT ERRORLEVEL 0 (
REM Do something to sleep here, pinging localhost or timeout
GOTO CheckVBox
)
REM VM Ready, go ahead and fire up other apps that depend on it
Note: you will need to have the path to the VBoxManage
executable in your PATH environment variable. On Windows, it is typically: C:\Program Files\Oracle\VirtualBox
Granted, you would probably want something more robust that just running ipconfig
to detect if the VM is really ready. I am not sure what services etc. you would depend on having available, but this might be good enough to get you over the hump and can certainly be adapted to check for service status etc.
Upvotes: 1