Introduction
Most Linux servers run background services such as web servers, databases, APIs, and monitoring tools. These services need to start automatically, restart when they fail, and be managed consistently.
Modern Linux distributions use systemd as their service manager. It provides a standard way to start, stop, restart, and monitor applications running in the background.
Whether you're deploying a .NET application, a Python API, or a Node.js service, understanding systemd is an important skill for managing production servers.
In this tutorial, you'll learn how to create and manage custom systemd services.
What Is systemd?
systemd is a system and service manager used by most modern Linux distributions.
Its responsibilities include:
Starting services
Stopping services
Restarting failed services
Managing system boot processes
Tracking service status
Example services:
Nginx
MySQL
Redis
ASP.NET Core APIThese services are typically managed through systemd.
Checking Service Status
To view the status of a service:
sudo systemctl status nginxExample output:
Active: active (running)This command shows:
Service status
Process ID
Recent logs
Startup information
Starting and Stopping Services
Start a service:
sudo systemctl start nginxStop a service:
sudo systemctl stop nginxRestart a service:
sudo systemctl restart nginxReload configuration:
sudo systemctl reload nginxThese commands are commonly used during deployments.
Creating a Custom Service
Suppose you have a .NET application.
Application location:
/var/www/myapiExecutable:
MyApi.dllCreate a service file:
sudo nano /etc/systemd/system/myapi.serviceService File Example
Add the following configuration:
[Unit]
Description=My ASP.NET Core API
[Service]
WorkingDirectory=/var/www/myapi
ExecStart=/usr/bin/dotnet /var/www/myapi/MyApi.dll
Restart=always
User=www-data
[Install]
WantedBy=multi-user.targetSave the file.
This tells systemd how to run your application.
Understanding Service File Sections
Unit Section
[Unit]
Description=My ASP.NET Core APIProvides information about the service.
Service Section
[Service]
ExecStart=/usr/bin/dotnet MyApi.dllDefines how the service runs.
Install Section
[Install]
WantedBy=multi-user.targetAllows automatic startup during boot.
Reload systemd
After creating or modifying a service:
sudo systemctl daemon-reloadThis reloads systemd configuration files.
Enable Auto Startup
Enable the service:
sudo systemctl enable myapiOutput:
Created symlinkThe application now starts automatically when the server boots.
Start the Service
Start the custom service:
sudo systemctl start myapiCheck status:
sudo systemctl status myapiIf everything is configured correctly:
Active: active (running)View Service Logs
systemd integrates with the journal.
View logs:
journalctl -u myapiView live logs:
journalctl -u myapi -fThis is extremely useful for troubleshooting.
Automatic Service Restart
One of systemd's most useful features is automatic recovery.
Example:
Restart=alwaysIf the application crashes:
Application Crash
↓
systemd Detects Failure
↓
Service RestartsThis improves application availability.
Real-World Example
Suppose you're hosting an ASP.NET Core API.
Without systemd:
Manual Start
↓
Server Reboot
↓
Application OfflineWith systemd:
Server Reboot
↓
systemd Starts Service
↓
Application OnlineThis makes deployments more reliable.
Common Mistakes
Forgetting daemon-reload
After modifying service files:
sudo systemctl daemon-reloadmust be executed.
Incorrect File Paths
Verify:
WorkingDirectory
ExecStartIncorrect paths are a common cause of failures.
Running as Root Unnecessarily
Instead of:
User=rootUse a dedicated application user whenever possible.
This improves security.
Best Practices
When using systemd:
Use dedicated service accounts.
Enable automatic restart.
Store logs properly.
Use descriptive service names.
Monitor service health.
Keep service files under version control when possible.
These practices improve maintainability and reliability.
Conclusion
systemd is the standard service manager for modern Linux systems and an essential tool for running production applications. It simplifies service management by providing commands for starting, stopping, monitoring, and automatically restarting applications.
Whether you're hosting ASP.NET Core APIs, Python services, Node.js applications, or background workers, systemd ensures your applications remain available and start automatically after system reboots. Learning how to create and manage custom services is a valuable skill for developers working with Linux servers.

Join the conversation! Your thoughts help the community grow.