Skip to content

Instantly share code, notes, and snippets.

@antoniocampos
Last active March 29, 2024 16:44
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save antoniocampos/2b6f81e923012a17671384b7b35a7ed5 to your computer and use it in GitHub Desktop.
Save antoniocampos/2b6f81e923012a17671384b7b35a7ed5 to your computer and use it in GitHub Desktop.
.net Core Systemd Service Example (Linux autostart you .net app)

First edit/create your .service file, with your favorite editor

use the example below...

nano /etc/systemd/system/MyService.service

Enable your service on boot

systemctl enable MyService.service

Start your service??

systemctl start MyService.service

Stop the service?

systemctl stop MyService.service

Want to know the service status??

systemctl status MyService.service

[Unit]
Description=MyService Description
[Service]
WorkingDirectory=/path/to/app/
ExecStart=/usr/bin/dotnet /path/to/app/App.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=log.antoniocampos
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
TimeoutStopSec=30
[Install]
WantedBy=multi-user.target
@barruka
Copy link

barruka commented Dec 3, 2022

IMPORTANT: The user set in User parameter MUST be in root group (must grant root access to the user):
sudo usermod -a -G root www-data

@HOSTED-POWER
Copy link

IMPORTANT: The user set in User parameter MUST be in root group (must grant root access to the user): sudo usermod -a -G root www-data

I would advise strongly against is for the sake of security, we used systemd user service and no such root access was needed.

@asepridwans
Copy link

@barruka , thanks for your info, almost one week i'am looking for what the reason my service not work, and finally my app.service can run automatically. Thanks a lot @barruka

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment