0

I need a simple batch command which will wait until specified service (actually, SQL Server) is finished starting. See, the bat file runs some non-service executables which attemp to connect to the SQL server on start. And they fail.

I tried to help (" /?" key) some Windows Shell commands but they do not seem to respond with an action I need.

AgentFire
  • 8,944
  • 8
  • 43
  • 90

1 Answers1

2

Unfortunately, this command doesn't exist. The sc can start and stop a service and it can query the status of a service (see this answer). But even when you query, you will only get "Service is running"; there is no way to tell how far the service got in its startup.

There are two workarounds:

  1. Sleep for a while
  2. Run a simple SQL command in a loop

The first approach will help but it won't completely solve the problem; when the server is under load, the startup can take longer.

For the second approach, use a loop which times out (pseudocode):

  1. Set counter to X
  2. Try to connect
  3. If connect succeeded, exit with success
  4. Decrement counter
  5. If counter <= 0 -> ERROR
  6. Sleep 1s
  7. Go to step 2

For additional safety, you could add a sc query in there to make sure the service didn't fail.

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820