且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-10-20 15:39:52

我认为没有针对此的跨平台解决方案.服务是针对特定平台的,即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应用程序:

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