.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