Linux 中的 .NET 核心服务权限被拒绝

Permission denied for .NET core service in Linux

我在 .net core 6 中创建了一个 worker 服务,例如 here 并将其部署在 Centos 中。它的单位是

[Unit]
Description=my test app

[Service]
Type=notify
ExecStart=/usr/bin/DaneshAfzar

[Install]
WantedBy=multi-user.target

systemctl start DaneshAfzar.service 引发错误:Job for DaneshAfzar.service failed because the control process exited with error code.有关详细信息,请参阅“systemctl status DaneshAfzar.service”和“journalctl -xe”

journalctl -xe 的输出: 在 EXEC 生成步骤失败 /usr/bin/DaneshAfzar:权限被拒绝

我对整个目录使用了 chmod 777,但错误仍然存​​在。

我们假设服务名为 myservice。我认为最好将 .net 服务部署到 /opt/myservice 目录或 /usr/local/myservice .

那么结构应该是这样的:

# Folder path
/opt/myservice

# path of the executable
/opt/myservice/myservice 
# or 
/opt/myservice/myservice.dll

只有文件 /opt/myservice/myservice.dll 必须是可执行的

服务文件可以是这样的:

[Unit]
Description=IG Render Service

[Service]
WorkingDirectory=/opt/myservice
ExecStart=/usr/bin/dotnet myservice.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=myservice
# Optionaly define a specific user to run the service
# User=someuser
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

希望这对您有所帮助。