我需要有关将net core console或asp.net应用程序托管为linux deamon的信息。 Microsoft.Hosting.WindowsService已经支持将应用程序作为Windows服务托管,但我需要类似于linux deamons的内容。
答案 0 :(得分:9)
我在RHEL上运行,因此选择编写自己的systemd单元文件。这是我与PostgreSQL结合使用的一个例子(因此是环境变量)。我出于显而易见的原因删除了敏感信息。
[Unit]
Description=My Sample Application
Documentation=
Wants=network.target
After=network.target
[Service]
User=dotnetuser
Group=dotnetuser
Nice=5
KillMode=control-group
SuccessExitStatus=0 1
Environment=MY_CONNSTRING=Server=localhost;Username=myUser;Password=myPass;Database=myDatabase
NoNewPrivileges=true
PrivateTmp=true
InaccessibleDirectories=/sys /srv -/opt /media -/lost+found
ReadWriteDirectories=/var/www/myapp
WorkingDirectory=/var/www/myapp
ExecStart=/opt/dotnet/dotnet run
[Install]
WantedBy=multi-user.target
该文件位于/etc/systemd/system
目录中,无论您希望使用" .service"命名该服务,都会将其命名。在它之后。例如,完整路径可能是/etc/systemd/system/aspnet-example.service
。
然后,您可以使用systemctl start aspnet-example
和systemctl stop aspnet-example
启动和停止服务。
将服务设置为在启动时启动:systemctl enable aspnet-example
配置文件中要指出的主要内容是:
用户和组不应该是root用户。我建议创建一个运行应用程序的新用户。
设置KillMode = control-group,以便将SIGTERM发送到运行服务的所有dotnet进程。
ReadWriteDirectory和WorkingDirectory指向Web应用程序的根目录。我以/var/www/myapp
为例。
ExecStart必须是dotnet二进制文件的绝对路径。 Systemd不支持相对路径。
https://docs.asp.net/en/latest/publishing/linuxproduction.html