Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart Service when Exception is thrown

I am writing a windows service that needs to be running 24/7. It is a pretty simple service that monitors a directory where files are dropped into and processes those files. I need to restart the service if an unhandled exception is thrown.

Is there a way for a service to restart itself in the event of an unhandled exception?

like image 242
Aaron M Avatar asked Jan 31 '26 10:01

Aaron M


1 Answers

The Services applet has many different recovery features:

Services Recovery

It can take different actions on the first, second, and subsequent failures:

  • Restart the service, after a configurable delay
  • Run a Program (passing command line parameters, possibly including the failure count)
  • Restart the Computer (after a configurable delay, and with a particular message being sent)

The program that runs should be able to look in the event log and see the reason for failure (especially if you log it), and should therefore be able to disable the service if the exception is one that is not recoverable.

And, of course, in the meantime, the service should be logging what's going on, which should enable any management tool to notify Operations of what's going on.

I agree that you should probably not configure "third and subsequent" to be "restart service", or you could wind up in a loop.

like image 107
John Saunders Avatar answered Feb 03 '26 09:02

John Saunders



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!