Score:-2

Schedule task in Windows 2012 R2 after all services are up

hu flag

We have a Windows 2012 R2 with some services configured as delayed start due to having dependencies on other services, but not being shown in the corresponding tab. If such services are not configured that way, they fail to start.

Since the server is powered up only during business hours we want to send a mail when all services are up and running but no such trigger exists in task scheduler.

A tasks was scheduled "when system starts" but obviously it does not work as desired.

How can this be accomplished? Is there a way to add a dependency on a service?

Score:0
hu flag

Not exactly what was needed but running this script when systems starts does the job

' VB Script Document
option explicit
Const MAIL_SERVICE = "mail_service"
Const VCS_SERVICE  = "our_vcs_service"

Dim args,status,objShell
Set args = Wscript.Arguments
' esperar el correo
status = wait_service (MAIL_SERVICE,10,6)
If (status = 0) Then
 ' esperar el control de versiones
 status = wait_service (VCS_SERVICE,10,6)
 Do While (status <> 0)
   status = wait_service (VCS_SERVICE,10,6) 
 Loop
End If

If (status = 0) Then
 Set objShell = CreateObject("WScript.Shell") 
 objShell.Run "sendEMail.exe ..." 
End If


Function wait_service (service_name,seconds,retries)
 Dim svcs 
 
 seconds = 1000*seconds

 ' no funciona
 wait_service = 1
 svcs = service_status (service_name)
 If (svcs = 1) Then
   Exit Function
 End If 
 
 ' iterar, esperando los segundos indicados 
 Do While (svcs <> 0) And (retries <> 0)
   retries = retries - 1
   WScript.Sleep seconds
   svcs = service_status (service_name) 
 Loop
 
 wait_service = svcs 
End Function

 
Function service_status (service_name)
 Dim wmi
 Dim svcs,svc
 ' no existe
 service_status = 1
 
 Set wmi = GetObject("winmgmts:\\.\root\cimv2")
 Set svcs = wmi.ExecQuery("Select * from Win32_Service where Name = '" & service_name & "'")
 If (svcs.Count = 0) Then
   Exit Function
 Else  
   ' no está ejecutando
   service_status = 2
   ' https://stackoverflow.com/questions/2378723/get-first-record-from-wmi-execquery'
   Set svc = svcs.ItemIndex(0)
   If (svc.State = "Running") Then
     service_status = 0
   End If
 End If
End Function  

The script waits for the mail server to be up and running and then also waits for the VCS server. If both are running the mail is sent

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.