.NET Core & Linux Service
Console
[Unit]
Description=Long running service/daemon created from .NET worker template
[Service]
# will set the Current Working Directory (CWD). Worker service will have issues without this setting
WorkingDirectory=/srv/WorkerApp
# systemd will run this executable to start the service
# if /usr/bin/dotnet doesn't work, use `which dotnet` to find correct dotnet executable path
ExecStart=/usr/bin/dotnet /srv/WorkerApp/WorkerApp.dll
# to query logs using journalctl, set a logical name here
SyslogIdentifier=WorkerApp
# Use your username to keep things simple.
# If you pick a different user, make sure dotnet and all permissions are set correctly to run the app
# To update permissions, use 'chown yourusername -R /srv/WorkerApp' to take ownership of the folder and files,
# Use 'chmod +x /srv/WorkerApp/WorkerApp' to allow execution of the executable file
User=yourusername
# ensure the service restarts after crashing
Restart=always
# amount of time to wait before restarting the service
RestartSec=5
# This environment variable is necessary when dotnet isn't loaded for the specified user.
# To figure out this value, run 'env | grep DOTNET_ROOT' when dotnet has been loaded into your shell.
Environment=DOTNET_ROOT=/usr/lib64/dotnet
[Install]
WantedBy=multi-user.target
sudo cp WorkerApp.service /etc/systemd/system/WorkerApp.service
sudo systemctl daemon-reload
sudo systemctl start WorkerApp
sudo journalctl -u WorkerApp -f
sudo systemctl enable WorkerApp
ASP.NET Core
dotnet add package Microsoft.Extensions.Hosting.Systemd
using WorkerApp;
using Microsoft.Extensions.Hosting;
IHost host = Host.CreateDefaultBuilder(args)
.UseSystemd()
.ConfigureServices(services =>
{
services.AddHostedService<Worker>();
})
.Build();
"UseSystemd()"
[Unit]
Description=Long running service/daemon created from .NET worker template
[Service]
Type=notify
# will set the Current Working Directory (CWD). Worker service will have issues without this setting
WorkingDirectory=/srv/WorkerApp
# systemd will run this executable to start the service
# if /usr/bin/dotnet doesn't work, use `which dotnet` to find correct dotnet executable path
ExecStart=/usr/bin/dotnet /srv/WorkerApp/WorkerApp.dll
# to query logs using journalctl, set a logical name here
SyslogIdentifier=WorkerApp
# Use your username to keep things simple.
# If you pick a different user, make sure dotnet and all permissions are set correctly to run the app
# To update permissions, use 'chown yourusername -R /srv/WorkerApp' to take ownership of the folder and files,
# Use 'chmod +x /srv/WorkerApp/WorkerApp' to allow execution of the executable file
User=yourusername
# ensure the service restarts after crashing
Restart=always
# amount of time to wait before restarting the service
RestartSec=5
# This environment variable is necessary when dotnet isn't loaded for the specified user.
# To figure out this value, run 'env | grep DOTNET_ROOT' when dotnet has been loaded into your shell.
# which dotnet
# multiple env use ' '(space) please
Environment=DOTNET_ROOT=/usr/lib64/dotnet
[Install]
WantedBy=multi-user.target
sudo systemctl stop WorkerApp # stop service to release any file locks which could conflict with dotnet publish
dotnet publish -c Release -o /srv/WorkerApp
sudo cp WorkerApp.service /etc/systemd/system/WorkerApp.service
sudo systemctl daemon-reload
sudo systemctl start WorkerApp
# For example, the following command will only print output with log level 4(Warning) (6)Information (3)Error and below: Using the unit-# flag (-u), we can filter down to our WorkerApp service.
# sudo journalctl -u WorkerApp -f -p 4
[Unit]
Description=ASP.NET Core Web API - MyApp
Documentation=https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx
[Service]
Type=notify
WorkingDirectory=/var/www/yourapp
ExecStart=/home/user/.dotnet /var/www/yourapp/YourApp.dll
Restart=always
RestartSec=10
# 生产环境推荐非 root 用户
User=www-data
Group=www-data
# 确保日志和临时文件权限正确
UMask=0022
# 环境变量(根据需要调整)
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
Environment=DOTNET_NOLOGO=true
Environment=DOTNET_ROOT=/home/user/.dotnet
Environment=ASPNETCORE_SHUTDOWNTIMEOUTSECONDS=10
# 日志配置
SyslogIdentifier=dotnet-yourapp
[Install]
WantedBy=multi-user.target

浙公网安备 33010602011771号