优雅地杀死在Linux上运行的.NET核心守护进程 [英] Killing gracefully a .NET Core daemon running on Linux
问题描述
我创建作为运行的Ubuntu 14.04机器上的后台程序.NET核心控制台应用程序。
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?
推荐答案
你想成为能够发送的 SIGTERM 到正在运行的进程:
You want to be able to send a SIGTERM to the running process:
kill <PID>
和过程应该是正确处理关闭。
And the process should handle it to shutdown correctly.
不幸的 .NET核心是不是有据可查的,但它能够处理Unix的信号(来自不同方式的单)。 GitHub的问题
Unfortunately .NET Core is not well documented, but it is capable of handling Unix signals (in a different fashion from Mono). GitHub issue
如果您使用的 Ubuntu的是新贵,你需要的是有一个init脚本发送的停止请求在杀的信号:的Example初始化脚本
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
添加到依赖您的 project.json
"System.Runtime.Loader": "4.0.0"
这会给你的 AssemblyLoadContext
然后就可以处理SIGTERM事件:
Then you can handle the SIGTERM event:
AssemblyLoadContext.Default.Unloading += MethodInvokedOnSigTerm;
请注意:
使用单声道的 Mono.Unix.Native.Signum.SIGTERM
这篇关于优雅地杀死在Linux上运行的.NET核心守护进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!