Summary

This article describes how to create a condition to make a job or step conditional on the state of a service process.

More Information

To set a condition on a service, you must first determine how the service is hosted and executed: in a separate process, or hosted in a common process that may also execute other services. To determine how the process is hosted, display the properties for the service in the Services tool in Windows and look at the "Path to executable" setting on the General page.

Services Hosted in a Unique Process

For most services, the "Path to executable" this will be a simple executable name. For example, for the adTempus service, the path is "c:\program files\arcana development\adtempus\adtempussrv.exe".

Such a service is hosted in its own process, and can be checked easily by using a Process Condition. Suppose that the service's path is "c:\program files\some service\service.exe".

  • To execute only if the service is running, create a Process Condition that is satisfied only if "service.exe" is running.
  • To execute only if the service is not running, create a Process Condition that is satisfied only if "service.exe" is not running.

You must be certain that only one instance of this process runs. To be sure, look at the process list in the Windows Task Manager. If there is more than one copy of "service.exe" (or whatever your service's executable is), then you cannot use this method; see the next section for an alternative approach.

Services Hosted in a Shared Process

Many services that are part of the Windows operating system are hosted in a shared process, "svchost.exe". For these services, the path is "c:\windows\system32\svchost.exe", followed by parameters that tell svchost.exe which service to execute. Because the process name is not specific to a service, you cannot monitor these services using a Process Condition. Instead, you can use a Script Condition to query Windows for the status of the service.

  1. To do this you must first determine the Service Name for the service you want to monitor. The Service  Name is the name that Windows uses to identify the service, and is often different from the Display Name, which is what you see in the Services tool. The Service Name can be found in the service's properties in the Services tool, right above the Display Name.
  2. Once you have determined the Service Name, add a new condition to your job or step to "Depend on the result of a script".
  3. In the Script Condition Properties window, select the appropriate option in the Condition Wait section.
  4. Click the Select button under Script to Evaluate.
  5. In the Select Script window, click New to create a new script.
  6. In the Script Properties window, make sure the Language is set to "VB.NET".
  7. In the Referenced Assemblies section, click the Add button. In the Add Assembly Reference window, select "System.ServiceProcess.dll" and click OK.
  8. In the code editing window, delete the existing code and paste in the following code:
Imports System
Imports System.Collections
Imports ArcanaDevelopment.adTempus.Server
Imports System.ServiceProcess 

Public Class UserScript
    Inherits ArcanaDevelopment.adTempus.ScriptEngine.UserScriptBase

    Public Overrides Function Run() As Object
	'If you want the job/step to run only if the service IS running, uncomment the following line (remove the single quote at the beginning of the line):
	'return IsRunning("myservice")
			
	'If you want the job/step to run only if the service IS NOT running, uncomment the following line (remove the single quote at the beginning of the line):
	'return not IsRunning("myservice")
    End Function

    Public Function IsRunning(serviceName as String) as Boolean
        Dim service As ServiceController

	'Open the requested service
        service = New ServiceController(serviceName)
		
	'check its status
        If service.Status = ServiceProcess.ServiceControllerStatus.Running Then
      	    'the service is running
	    adTempus.LogMessage(MessageTypeEnum.Informational,1,serviceName & " service is running")
	    Return True
        Else
            'the service is not running
	    adTempus.LogMessage(MessageTypeEnum.Informational,0,serviceName & " service is not running")
            Return False
        End If
    End Function
End Class
  1. Edit the code to replace "myservice" with the Service Name you found in Step 1.
  2. Uncomment either line 11 or line 14 of the code, depending on how you want the condition to work (see the comments in the code).
  3. Click OK, then OK, then OK to return to the job properties and finish editing your job.