如何从 .NET Core 控制台应用程序创建 Linux 守护进程? [英] How do you create a Linux daemon from a .NET Core console application?

查看:35
本文介绍了如何从 .NET Core 控制台应用程序创建 Linux 守护进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯于使用 Topshelf 创建 Windows 服务.随着 .NET Core 和跨平台的前景,这引发了许多有趣的场景:

I'm used to creating Windows services using Topshelf. With .NET Core and the prospect of going cross-platform, this raises a number of interesting scenarios:

  1. 鉴于 Topshelf 尚不支持 .NET Core,我该如何为 .NET Core 创建 Windows 服务?(一种方法可能是创建一个常规的 .NET Core 控制台应用程序并使用 NSSM 安装它,但这不提供启动/停止挂钩,因此无法正常停止服务).
  2. 你如何在 Linux 上做同样的事情?没有 Windows 服务,但有守护进程的概念.这个答案提供了一种基本方法,但需要额外的工作并且依赖于某些底层软件.
  3. 上面的 #1 和 #2 是否可以使用跨平台方法来完成,或者是否有必要针对每个平台(例如使用预处理器指令)来解决这个问题?
  1. Given that Topshelf does not yet support .NET Core, how can I create Windows services for .NET Core? (One approach could be to create a regular .NET Core console application and install it with NSSM, but that doesn't provide hooks for Start/Stop, so there is no way to gracefully stop the service).
  2. How do you do the same thing on Linux? There are no Windows services, but there is the concept of daemon processes. This answer provides a basic approach, but requires additional work and depends on certain underlying software.
  3. Can #1 and #2 above be done using a cross-platform approach, or is it necessary to tackle this per platform (e.g. with preprocessor directives)?

以上主要只是上下文.出于这个问题的目的,我想知道我需要采取哪些步骤才能使用 .NET Core 在 Linux 上运行等效的 Windows 服务.如果这可以跨平台以统一的方式完成,那就更好了.

The above is mainly just context. For the purpose of this question, I'd like to know what steps I need to take in order to run the equivalent of a Windows service on Linux, using .NET Core. If this can be done in a unified way across the platforms, even better.

推荐答案

我认为没有跨平台的解决方案.服务非常特定于平台,AFAIK.

I dont think there is a cross platform solution for this. Sevices are pretty platform specific, AFAIK.

对于 #2,如果您想在 systemd 下运行 .NET Core,您应该能够在不更改任何代码的情况下执行此操作.您基本上需要做的就是发布您的应用程序,然后创建一个 systemd unit 文件来描述您的守护进程.systemd 然后将处理启动、重新启动和终止您的应用程序.

For # 2, you should be able to do this without any code changes if you want run .NET Core under systemd. All you basically need to do is publish your application, and then create a systemd unit file to describe your daemon. systemd will then handle starting, restarting and killing your applications.

这里有一个 systemd 单元文件示例,用于将 ASP.NET Core 应用程序作为服务运行:https://docs.microsoft.com/en-us/aspnet/core/publishing/apache-proxy#monitoring-our-application

There is an example of a systemd unit file here to run a ASP.NET Core application as a service: https://docs.microsoft.com/en-us/aspnet/core/publishing/apache-proxy#monitoring-our-application

[Unit]
Description=Example .NET Application

[Service]
WorkingDirectory=/var/aspnetcore/hellomvc
ExecStart=/usr/bin/dotnet /var/aspnetcore/hellomvc/hellomvc.dll
Restart=always
RestartSec=10
SyslogIdentifier=dotnet-example
User=apache
Environment=ASPNETCORE_ENVIRONMENT=Production 

[Install]
WantedBy=multi-user.target

这篇关于如何从 .NET Core 控制台应用程序创建 Linux 守护进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆