Killing gracefully a .NET Core daemon running on Linux

user4388177 picture user4388177 · Jul 10, 2016 · Viewed 22k times · Source

I created a .NET Core console application running as a daemon on a Ubuntu 14.04 machine.

I want to stop the service without forcing it, being able to handle a kill event.

How can I achieve this?

Answer

Marc Sigrist picture Marc Sigrist · Nov 24, 2017

.NET Core has considerably evolved since @Stefano's answer a year ago. In .NET Core 2.0, you can now use the well-known AppDomain.CurrentDomain.ProcessExit event instead of AssemblyLoadContext.Default.Unloading. It works fine for console applications on Linux, also in Docker.