Stop Windows Services in Particular Order

written by SystemAdmin)

I’m looking for the best way to shut down a particular set of services on a Windows 2003 server in a specific order. What would be the best way to go about automating that? I’ve toyed with creating a baseline that performs the Service Stop task, but when you add the component it doesn’t prompt you for which service you’re looking to shut down. Any help would be appreciated. THANKS!

written by SystemAdmin)

Sounds like you have something particular in mind. A set of discreet services that always need to come down in a specific order.

A custom Task or set of Tasks might be a better way to handle something like that.

You could make the Task relevant only to computers that have the particular software you are concerned about, with the services running, then have the action shut down each service in order then shutdown the computer as the last step (use the Post-Action tab for that).

Looking at the

Stop Service

Task, it should be fairly simple for you to create a copy of that Task to stop the services you want, in the order you want. it just builds a batch file on the fly with the appropriate

NET STOP

commands, then use waithidden to execute it. The Task uses the RunQuiet.exe utility so nothing shows while it’s shutting down the services.

If you really want to do it as a Baseline, you can create a separate task for each service, then add them to a baseline in the order you want them shutdown.

Depending on how many unique configurations you want to handle, a single task should do it.

Just repeat the line …

waithidden “{pathname of client folder of site “BESSupport” & “\RunQuiet.exe”}” netquiet.bat stop “{parameter “ServiceName” of action}”

line for each service replacing

{parameter “ServiceName” of action}

with the name of the service to stop.

written by SystemAdmin)

You must be looking over my shoulder because that is exactly what I have been doing for the past couple of hours. I took the stop service action language and explicitly created a task for each of the services I was looking to stop. Then I created a baseline and put the services in the order I wanted.

BUT, there was one issue. My last service stopped before my 2nd to last service because the time it took to stop was shorter for the last one. I need the next service in line to stop ONLY when the one before it has COMPLETELY stopped, not just after the stop command has been executed. So I need to incorporate some kind of check in the process.

For the baseline I think it only looks to see if the component ahead of it has had its action language executed, not whether or not the service has actually completely stopped.

So now the question is how do I incorporate some sort of check?

I did create a crude way of doing what I need to do though. I created a separate “Stop Service” action for the services in question and just spaced the times out wide enough to give them enough time to finish stopping. Crude, but it works.

written by JasonHonda)

Something that might help is putting an line after the stop command

pause while {exists running service “BESClient”}

This at least in my head will wait for it to stop. This should obviously be tested and may be bad if the service does not actually stop ever.

written by jeremylam)

You can use “pause while {…}” and “continue if {…}”, but as Jason said you need a time limit on the pause while or else the client may get stuck.

Here’s how you can set a time limit:

dos net stop BESRelay
parameter "startTime" = "{apparent registration server time}"
 
// Pause until BESRelay terminates or 2 minutes has passed.
pause while {(not exists running service "BESRelay") AND (apparent registration server time - time (parameter "startTime")) < 2*minute}

written by jeremylam)

And then after that last line:

continue if {not exists running service "BESRelay"}

This will terminate the action if it has been two minutes and the relay is still running.