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?
.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.
AppDomain.CurrentDomain.ProcessExit
only allows for 2 seconds of cleanup, and there is no way to override it using managed code.
You want to be able to send a SIGTERM to the running process:
kill <PID>
And the process should handle it to shutdown correctly.
Unfortunately .NET Core is not well documented, but it is capable of handling Unix signals (in a different fashion from Mono). GitHub issue
If you use Ubuntu with Upstart, what you need is to have an init script that sends the the kill signal on a stop request: Example init script
Add a dependency to your project.json:
"System.Runtime.Loader": "4.0.0"
This will give you the AssemblyLoadContext.
Then you can handle the SIGTERM event:
AssemblyLoadContext.Default.Unloading += MethodInvokedOnSigTerm;
Note:
Using Mono, the correct way of handling it would be through the UnixSignal: Mono.Unix.Native.Signum.SIGTERM
EDIT:
As @Marc pointed out in his recent answer, this is not anymore the best way to achieve this. From .NET Core 2.0 AppDomain.CurrentDomain.ProcessExit
is the supported event.
dotnet <DllName>.dll
(without run) that should run just one process and you can try killing that one.
Aug 16, 2016 at 11:57
dotnet library.dll
and the stop one:kill dotnet library.dll
.dotnet app.dll
. For us, this caused an issue. To mitigate it, we addedexec dotnet app.dll
which helped to propagate SIGTERM signal to dotnet application, and not let shell script handle the SIGTERM directly.