How to create a Linux daemon from a .NET Core console application?

I use to create Windows services using Topshelf. With .NET Core and the prospect of moving to a cross-platform platform, a number of interesting scenarios arise:

  • Given that Topshelf does not yet support .NET Core, how can I create Windows services for .NET Core? (One approach may be to create a regular .NET Core console application and install it using NSSM, but this does not provide intercepts to start / stop, so there is no way to gracefully stop the service).
  • How do you do the same in Linux? There are no Windows services, but there is a concept for daemon processes. This answer provides a basic approach, but requires additional work and depends on the specific software.
  • Is it possible to execute # 1 and # 2 using a cross-platform approach, or is it necessary to solve this problem on a platform (for example, with preprocessor instructions)?

The foregoing is mainly context. For the purposes of this question, I would like to know what steps I need to take to start the Windows service equivalent on Linux using .NET Core. If this can be done in a unified way on platforms, even better.

+5
source share
1 answer

I don’t think there is a cross-platform solution for this. Sections are fairly platform specific, AFAIK.

For # 2, you can do this without any code changes if you want to run .NET Core under systemd . All you have to do is publish your application and then create a systemd unit file to describe your daemon. systemd will handle the launch, restart, and destruction of your applications.

The following is an example systemd file for starting an ASP.NET Core application as a service: https://docs.microsoft.com/en-us/aspnet/core/publishing/apache-proxy#monitoring-our-application p>

 [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 
+4
source

All Articles