如何从.NET Core控制台应用程序创建Linux守护程序?

时间:2021-05-02 23:18:15

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:

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

  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. 鉴于Topshelf尚不支持.NET Core,我如何为.NET Core创建Windows服务? (一种方法可能是创建一个常规的.NET Core控制台应用程序并使用NSSM安装它,但是它不提供启动/停止的挂钩,因此无法正常停止服务)。

  3. 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.
  4. 你如何在Linux上做同样的事情?没有Windows服务,但有守护进程的概念。这个答案提供了一种基本方法,但需要额外的工作并依赖于某些底层软件。

  5. 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)?
  6. 可以使用跨平台方法完成上述#1和#2,还是有必要针对每个平台解决这个问题(例如,使用预处理器指令)?

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.

以上主要是上下文。出于这个问题的目的,我想知道我需要采取哪些步骤来使用.NET Core在Linux上运行相当于Windows的服务。如果这可以跨平台以统一的方式完成,甚至更好。

1 个解决方案

#1


5  

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

我不认为有这样的跨平台解决方案。服务非常适合平台,AFAIK。

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.

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

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

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

[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

#1


5  

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

我不认为有这样的跨平台解决方案。服务非常适合平台,AFAIK。

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.

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

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

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

[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