'how to force stop and not restart a service

For a service written in c#.

We have it set to auto-restart so if it crashes it will restart itself.

This is fine, but we also want a way to force stop the service (the service could be deadlocked). This would happen once a week before taking a database backup.

If we use "net stop" the service might not shut down.

If we do a taskkill this will kill the service but then it will start up again in one minute.

Any ideas on how to get both use cases working?

We want to auto-restart if the service crashes, and we want to be able to run an unattended process that will stop the service (no matter what), take a backup of the database, and then restart the service after that.



Solution 1:[1]

http://www.codeproject.com/Articles/31688/Using-the-ServiceController-in-C-to-stop-and-start

You need to add a library, then it's just: service.Stop() and service.Start()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MrFox