7

How do you make your stop/start service scripts gracefully handling the services that get stuck in STOP_PENDING or START_PENDING...

Related with SC STOP and START. Stop and Start a service via batch or cmd file?

I have one aproach in mind, but I don't like it.

Community
  • 1
  • 1
João
  • 2,296
  • 5
  • 20
  • 30

3 Answers3

6

This is the way i'm approaching the stop.

  • Stop: sc %host% stop %service% || ECHO ERROR! && EXIT -1
  • Loop waiting for state to be STOPPED. Loop with a maximum number of iterations (e.g. 30). Testing if service is stopped (e.g. sc %host% query %service% | find /i "STOPPED"). Wait some seconds in each iteration (e.g. 10). Reaching the maximum iterations, try to kill the process using taskkill (e.g. work the output of sc queryex Alerter | find /i "PID").
João
  • 2,296
  • 5
  • 20
  • 30
5

First open service.msc, in the properties dialog for your targeted service go to the Recovery Tab *(run -> services.msc -> right-click Target-Service -> Properties -> Recovery tab*)

Make sure the First\Second\Subsequent failures are all set to : Take No Action. enter image description here

Get the PID of your target service using: sc queryex ServiceName:

Kill the process using taskkill /f /pid your_PID_number

AlexandruC
  • 3,527
  • 6
  • 51
  • 80
3

For me sometimes a 'Windows service' is hanging when I have the window services.msc open. I can't kill the process, neither use net stop or sc stop.

But when I close the services.msc window, the service immediately stops.

Beetee
  • 475
  • 1
  • 7
  • 18