
Mastering Custom Systemd Services in Linux: A Comprehensive Guide
Systemd stands as a robust and dynamic init system across various Linux distributions. It facilitates program execution, resource management, and overall system control. This guide will empower you to harness the full potential of Systemd by creating personalized service units in Ubuntu.
Understanding Systemd Service Units
At its core, a service unit is a standardized file that outlines the configuration necessary to execute a specific application. It encapsulates metadata about the program, execution directives, and details about Systemd’s access framework during the sessions.
Every daemon in a Systemd-supported environment is governed by a service file. For example, OpenSSH operates using the ssh.service
unit located in /etc/systemd/system/
to dictate its functioning in Debian and Ubuntu systems.

Generally, a service unit file is structured into three primary sections: Unit
, Service
, and Install
. The Unit
section denotes the application’s metadata and dependencies, while the Service
section specifies its location and execution methodology. Conclusively, the Install
section clarifies when the application can be initiated by Systemd.
Creating a System-Level Custom Service
Custom services are invaluable for automating tasks requiring elevated privileges or utilizing Systemd timers. A typical example involves ensuring that your Minecraft server starts reliably following a system restart.
Steps to Create Your Custom System-Level Service
- Create the Systemd Unit File: Begin with the creation of a new unit file in your home directory.
- Insert Configuration: Add the following template inside the new unit file. This serves as a valid foundational configuration:
- Customize Your Service: Adjust the
Description
andExecStart
fields with details pertinent to your service. - Move the File: Save and transfer it to the service directory:
- Reload Systemd Daemon: Execute the command to reload services:
- Start Your Service: Initiate your custom system-level service:
- Verify Functionality: Confirm your service is operational:
nano ~/my-system-app.service
[Unit] Description=My First Service After=network.target [Service] Type=simple ExecStart=/path/to/bin Restart=always [Install] WantedBy=multi-user.target
sudo cp ~/my-system-app.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl start my-system-app.service
systemctl status my-system-app.service

Establishing a User-Level Custom Service
Systemd supports the creation of rootless services using Systemd-user, allowing non-root users to handle personal applications effectively while enhancing security by minimizing root access.
How to Create Your User-Level Custom Service:
- Set Up the Unit File: Create a new unit file in the home directory.
- Insert Basic Configuration: Use the following framework in the unit file:
- Adjust ExecStart: Ensure the path specified in
ExecStart
is accessible to your user account. - Create User’s Local Systemd Directory:
- Copy the Unit File: Transfer your custom service unit to the local directory:
- Reload Systemd for User: Verify Systemd checks the user directory for changes:
- Check Service Status: Ensure your user-service is correctly recognized:
nano ~/my-user-app.service
[Unit] Description=My First User Service After=graphical-session.target [Service] Type=simple ExecStart=/path/to/bin [Install] WantedBy=default.target
mkdir -p ~/.config/systemd/user/
cp ~/my-user-app.service ~/.config/systemd/user/
systemctl daemon-reload --user
systemctl --user status my-user-app.service

Optimizing Your Custom Systemd Service
Systemd’s flexibility allows users to finely tune service parameters and behaviors.
Integrating Environment Variables
Environment variables enrich Linux programs with additional context without modifying config files, and Systemd supports their incorporation in service units.
- Disable the Service: Start by disabling the service you wish to modify:
- Edit Your Service File: Open the unit file in your preferred text editor:
- Modify the Service Section: Under the
[Service]
section, include: - Reload Daemon: Save and reload the Systemd daemon to apply changes:
- Restart the Service: Restart using your new environment variable:
systemctl --user disable --now my-user-app.service
sudo nano ~/.config/systemd/user/my-user-app.service
Environment="VARIABLE_NAME=VALUE"
systemctl --user daemon-reload
systemctl --user start my-user-app.service

Limiting Resource Usage
Systemd provides mechanisms for regulating resource usage of services to ensure fair utilization.
- Disable the Target Service: Start off by disabling it completely:
- Edit the Unit File: Open the service file with:
- Add Resource Limits: For instance, to limit memory:
- Reload Unit Changes: Reload your new settings:
- Monitor Resource Usage: Track the service performance with:
sudo systemctl disable my-system-app.service
sudo nano /etc/systemd/system/my-system-app.service
MemoryHigh=SIZE
sudo systemctl daemon-reload
systemd-cgtop

Conclusion
Exploring Systemd unlocks significant potential for process management in Linux. By creating custom services, from automating tasks to managing resource efficiency, you can significantly enhance your system’s interoperability and performance. Delve deeper into Systemd and its wide-ranging capabilities, and consider exploring Run0’s comparative advantages over Sudo for a more nuanced understanding.
Image credit: Mikhail Fesenko via Unsplash. All modifications and screenshots by Ramces Red.
Frequently Asked Questions
1. What are the advantages of using Systemd services over traditional init systems?
Systemd offers parallel service startup, dependency management, and a unified approach to service handling, enhancing overall performance and reliability compared to traditional init systems.
2. Can I create multiple services for a single application?
Yes, you can create multiple service units for a single application to manage different aspects, such as starting with different configurations or during specific conditions.
3. How do I troubleshoot issues with my Systemd services?
You can troubleshoot Systemd services using commands like journalctl -u my-service-name.service
to check logs, and systemctl status my-service-name.service
to investigate the current status and error messages.
Leave a Reply ▼