413X
Posts: 11
Joined: Wed Jun 13, 2018 9:46 am

Wait for Service to be running and then proceed

Fri Jul 06, 2018 9:56 am

Hello

I am currently experiencing some issues with my installation. I believe it is that my service has not started completely before next custom action is executed.

In my custom action sequence it looks like this:

1. Start Service A.
2. Start Service B.
3. Do stuff with Service A. Fails.
4...12.
13. Stop Service A.
14. Stop Service B.

Potential solution 1: How do I detect that the service is running before proceeding?
Potential solution 2: How do I make the installation wait X amount of seconds before it has gotten it's chance to start?

Daniel
Posts: 8237
Joined: Mon Apr 02, 2012 1:11 pm
Contact:  Website

Re: Wait for Service to be running and then proceed

Fri Jul 06, 2018 12:51 pm

Hello,
Potential solution 1: How do I detect that the service is running before proceeding?
We do not have any built-in support for this. A workaround solution could be to check this within the implementation code of your next custom action.
Potential solution 2: How do I make the installation wait X amount of seconds before it has gotten it's chance to start?
For this you could create another custom action which will just run a system sleep command.

Hope this helped.

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

413X
Posts: 11
Joined: Wed Jun 13, 2018 9:46 am

Re: Wait for Service to be running and then proceed

Fri Jul 06, 2018 3:12 pm

Heh, it appears I was wrong what I needed so I coded this little vbscript in vain. So in case anyone needs it. Here you go. Example case: cscript script.vbs "servicename1" "servicename2" "servicename3etc"

Code: Select all

' Checks if services exists and is running 

' Configurable variables:
Dim objAllowedAttempts
objAllowedAttempts = 20
Dim objSleepDuration
objSleepDuration = 100


Dim objArgs
Set objArgs = Wscript.Arguments
Dim objAllowedAttemptsIterator
objAllowedAttemptsIterator = 0

If( objArgs.Count > 0 ) Then
	strComputer = "."
	Set objWMIService = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

	' Declarations
	Dim objIter
	Dim objSearchString
	
	' objIter iterates each service
	objIter = 0
	While objIter <> objArgs.Count
		'Searching after the Service in SQL format.
		objSearchString = "Select * from Win32_Service Where Name='" & objArgs(objIter) & "'"
		Set colRunningServices = objWMIService.ExecQuery(objSearchString)  
		nItems = colRunningServices.Count  
		
		If nItems > 0  Then
			For Each objItem in colRunningServices 
				If objItem.State = "Running" Then 
					Wscript.Echo("Active")
					objIter = objIter + 1
					objAllowedAttemptsIterator = 0
				End If
			Next
		End If
		WScript.Sleep objSleepDuration
		objAllowedAttemptsIterator = objAllowedAttemptsIterator + 1
		' If attempts reached max. Assume we are done.
		If objAllowedAttemptsIterator = objAllowedAttempts Then
			objIter = objArgs.Count
			Wscript.Echo("Done")
		End If
	Wend
End If

Daniel
Posts: 8237
Joined: Mon Apr 02, 2012 1:11 pm
Contact:  Website

Re: Wait for Service to be running and then proceed

Mon Jul 09, 2018 8:48 am

Hello,

Thank you for your follow up on this and for sharing with our community your solution. Certainly this will be useful to other customers too.

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”