views:

225

answers:

6

Hi,

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

A: 

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

jeffamaphone
+1  A: 

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
A: 

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
A: 

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.

Cannonade
A: 

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
I guess this comment is more .NET service specific - not sure how non-.NET services are stopped internally...
Andy White
A: 

You might want to check out this other question here on StackOverflow.

Also, relying on the network for this type of monitoring can be problematic, so it is often better to do the monitoring from each individual machine with a dedicated windows service monitoring utility.

ExtraLean