1

I want to schedule a restart of my custom services automatically using a batch file with net stop, net start.

When net stop runs does it abort anything that is being done immediately?

Just wondering what will happen if in the middle of processing?

Malcolm

Malcolm
  • 12,524
  • 28
  • 89
  • 125
  • If your services need to be restarted instead of just continuing to run indefinitely, then there's probably something wrong with them. Look into fixing that problem instead of having to restart them all the time. – Rob Kennedy Mar 09 '09 at 03:25

5 Answers5

1

It will call into your code asynchronously and it will be up to you to deal with it. You could enact a clean or abort as you see fit.

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
1

It really depends on how the service is implemented. "net stop" essentially calls into the service and says "would you kindly stop". Most services will comply with this command and stop in a timely fashion. However there are the bad services which do not comply and refuse to stop. In this case, net stop will take no further action.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
0

It really depends on the service. I suspect most will try to get into a good state before stopping. It isn't a kill.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
0

A service registers to receive events (via RegisterServiceCtrlHandler). When you do a net stop the registered callback will receive a callback with the SERVICE_CONTROL_STOP operation. How the service responds to that callback is up to the service implementation. It would make sense for the service to do regular application shutdown processing.

RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
0

Like the others said, when you call net stop, it will invoke the OnStop in the Windows Service. If the OnStop does not kill all the threads in the app, or doesn't shut everything down properly, your service might not stop. (I've seen this happen in one of our WCF services: we didn't close the ServiceHost in OnStop, and therefore, the app would not stop at our command - we'd have to kill the process by hand.)

One common pattern I've seen is to try calling stop on the service, and if it doesn't die within a timeout (10 seconds), kill the process by force. As an alternative to batch files, PowerShell has some pretty good support for dealing with services.

Andy White
  • 86,444
  • 48
  • 176
  • 211
  • I guess this comment is more .NET service specific - not sure how non-.NET services are stopped internally... – Andy White Mar 09 '09 at 02:43