To create a systemd service file for your application on Linux, follow this template. This example assumes your service resides in /opt/hngunitystore and runs under the user storeuser.
1. Create a systemd Service File
You can create the service file at /etc/systemd/system/hngunitystore.service.
sudo nano /etc/systemd/system/hngunitystore.service
2. Service File Configuration
Here’s the configuration for the systemd service:
[Unit]
Description=HNG Unity Store Service
After=network.target
[Service]
# The user that runs the service
User=storeuser
# The working directory where the service resides
WorkingDirectory=/opt/hngunitystore
# The executable to run
ExecStart=/usr/bin/dotnet /opt/hngunitystore/HNGUnityStore.dll
# Restart the service on failure
Restart=on-failure
# Optional: Set time to wait before restarting the service
RestartSec=5
# Environment file (optional) if you need to pass environment variables
EnvironmentFile=/opt/hngunitystore/.env
# Logs
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=hngunitystore
[Install]
# This service will start after the system boots
WantedBy=multi-user.target
Breakdown:
- User: Runs the service under the
storeuseraccount. - WorkingDirectory: Specifies where the service files are located (
/opt/hngunitystore). - ExecStart: The command to execute. In this case, it runs the .NET application located at
/opt/hngunitystore/HNGUnityStore.dll. - Restart: Ensures the service is restarted if it fails.
- Logs: Logs the output and errors to the system’s log system (
syslog).
3. Enable and Start the Service
After creating the service file, enable and start the service:
# Reload systemd to recognize the new service
sudo systemctl daemon-reload
# Enable the service to start at boot
sudo systemctl enable hngunitystore
# Start the service immediately
sudo systemctl start hngunitystore
# Check the status of the service
sudo systemctl status hngunitystore
4. Logs
To check the logs of the service, you can use:
journalctl -u hngunitystore
This will display the logs that were sent to syslog based on SyslogIdentifier=hngunitystore.
By setting this up, your HNGUnityStore service will run as a background process on Linux managed by systemd.
