Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. 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)?

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.

like image 597
Gigi Avatar asked May 16 '17 11:05

Gigi


People also ask

Can .NET Core application run on Linux?

NET Core runtime allows you to run applications on Linux that were made with .


1 Answers

I dont think there is a cross platform solution for this. Sevices are pretty platform specific, 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.

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
like image 123
omajid Avatar answered Oct 01 '22 04:10

omajid