Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

We have this open source application that has three (3) services. For the purpose of this question, let's call them A, B, and C.

Now, they have to be shut down in a specific order. That order would be A then B then C.

If B shuts down before A then we run into all kinds of problems. Same is true if C shuts down before B or A. Plus, each service can take a different amount of time to shut down due to how many users were using it.

Oh, this need to be wrapped up in a DOS batch file or something a non-techy user could just double-click to initiate. (PowerShell is not out of the question but I've never used it). Also, I'm a C# coder so that could be used too.

Anyway, when the restart is initiated the following needs to happen:

1) Initiate shutdown of service A
2) When service A is down, it should trigger the shutdown of service B
3) When service B is down, it should trigger the shutdown of service C
4) When service C is down, it should trigger the START UP of service A
5) When service A is UP, it should trigger the START UP of service B
6) When service B is UP, it should trigger the START UP of service C

So as you can see, each stop/start needs to wait on the previous to be completely finished before moving on. And since each service can take a few seconds to a few minutes, we can't use any kind of timing tricks.

Suggestions greatly appreciated.


share|improve this question
up vote 2 down vote accepted

You could do this using PowerShell. To stop a service, you use the Stop-Service command. Starting a service is with the Start-Service command. So, you could create a script that had a few entries like the following for each service you need to stop, in order:

Stop-Service ServiceA
do { Start-Sleep -Milliseconds 200}
until ((get-service ServiceA).status -eq 'Stopped')

and to start the services:

Start-Service ServiceA
do { Start-Sleep -Milliseconds 200}
until ((get-service ServiceA).status -eq 'Running')

You could then string 6 of those code blocks together in a script, save it as a .ps1 file, and run it from the powershell prompt as so:

PS C:\> C:\Path\To\Script\script.ps1
share|improve this answer
Not being familiar with PowerShell, does this script simply poll the service every 200ms until it reports stopped or running? Would this not cause some performance issues with other, non related services on the server? I mean, it looks like it would work but I would hate to affect other running services or anything like that. thanks – cbmeeks Sep 7 '12 at 17:05
I suppose it depends on how long the services take to stop and start. If the services take a long time, I would change the loop's interval time to something slower, probably closer to how long you expect the service to end. For example, if you expect the service to take 30 seconds to stop, you could change it to {Start-Sleep -Seconds 15} - on average the script will check 2-3 times per service. – Steve G Sep 7 '12 at 17:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.