1

We have a custom application that messes with a LOT of 3rd party resources. When terminating, we need to gracefully "give up the lease" so to speak.

The app itself is written in .NET Core (and targets true multiplatform run-ability). We use Systemd's ExecStop mechanism in Linux to call our custom helper script (myapp cli shutdown).

That helper script exists in Windows too, but we're unsure how to actually tie it in with Windows' service management. We use NSSM to keep the service itself up continuously, but are open to options if this can be achieved.

I tried googling / searching StackExchange about a custom stop command on Windows, but have come up empty.

Does anyone know how we can pull this off?

Paul S.
  • 13
  • 2

1 Answers1

0

NSSM's "Shutdown" tab settings control what happens when the service receives a stop request. The Control-C and Terminate process options are unlikely to be useful, but since your application is in .NET it should be able to receive and respond to WM_CLOSE and/or WM_QUIT. I imagine this is also possible in .NET Core, though I'm not sure of the details.

The Apache Commons Daemon might be a better fit for you. Although it is designed primarily for services written in Java, it can also run arbitrary executables, and the --StopImage option allows you to run a command line when the service receives a stop request.

It would however be more elegant for your application to actually be a service rather than using a third-party wrapper such as NSSM or the Apache Commons Daemon. That's easy in .NET, harder in .NET Core, but a Google search turned up How to Create .NET Core Windows Services with Visual Studio 2017 which should get you started.

If you need more detail about implementing a service directly, you should probably ask on Stack Overflow, since this is more about Windows programming than systems administration.

Harry Johnston
  • 6,005
  • 4
  • 35
  • 52
  • Thank you for the insight, we'll look into the options you shared. Will note the StackOverflow suggestion too. – Paul S. Sep 11 '18 at 03:37