Operations 8 min read

Mastering systemd: How to Create, Configure, and Manage Custom Linux Services

This guide explains how to create custom systemd service unit files, configure permissions, environment variables, logging, and resource limits, and shows how to start, stop, and monitor services using systemctl, with detailed examples and advanced options for Linux administrators.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering systemd: How to Create, Configure, and Manage Custom Linux Services

Introduction

systemd is the default init system and service manager on modern Linux distributions. It handles system boot tasks, process supervision, and logging, and allows users to define custom services for specific tasks.

Creating a Service Unit File

To define a new service, create a unit file with a .service extension under /etc/systemd/system. A basic unit file contains three sections: [Unit]: metadata such as Description and dependencies. [Service]: execution parameters like ExecStart, Type, Restart, etc. [Install]: installation information such as WantedBy.

Example:

[Unit]
Description=My Custom Service
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/my_service
Restart=always

[Install]
WantedBy=multi-user.target

Configuring the Service

1. File Permissions and Paths

Ensure the executable has proper permissions and resides in a standard location such as /usr/local/bin or /opt.

sudo chmod +x /usr/local/bin/my_service

2. Environment Variables

Set variables directly in the unit file with Environment= or load them from a file with EnvironmentFile=.

[Service]
Environment="VAR1=value1"
Environment="VAR2=value2"
[Service]
EnvironmentFile=/etc/my_service.env

3. Logging

Redirect service output to the system log using StandardOutput and StandardError.

[Service]
StandardOutput=syslog
StandardError=syslog

Starting and Managing the Service

Use systemctl to control the service.

Start: sudo systemctl start servicename Stop: sudo systemctl stop servicename Restart: sudo systemctl restart servicename Status:

sudo systemctl status servicename

Advanced Usage

1. Pre‑ and Post‑Execution Hooks

Define commands that run before or after start/stop with ExecStartPre, ExecStartPost, ExecStop, and ExecStopPost.

[Service]
ExecStartPre=/bin/mkdir -p /var/run/my_service
ExecStartPost=/bin/chmod 755 /var/run/my_service
ExecStop=/bin/rm -rf /var/run/my_service
ExecStopPost=/bin/rmdir /var/run/my_service

2. Resource Limits

Limit CPU, memory, and task count with directives such as CPUShares, MemoryLimit, and TasksMax.

[Service]
CPUShares=512
MemoryLimit=1G
TasksMax=1000

3. Private Temporary Filesystem

Enable an isolated /tmp for the service with PrivateTmp=yes.

[Service]
PrivateTmp=yes

4. System Logging

Configure StandardOutput and StandardError to syslog so that all output is captured by the system logger.

[Service]
StandardOutput=syslog
StandardError=syslog

Complete Example

A full unit file that runs a simple script:

[Unit]
Description=My Custom Service
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/my_script.sh
Restart=always

[Install]
WantedBy=multi-user.target

The script /usr/local/bin/my_script.sh could contain:

#!/bin/bash
echo "Hello, world!" >> /var/log/my_service.log

Conclusion

By creating and configuring systemd unit files, Linux administrators can reliably manage custom services, control their lifecycle, apply resource constraints, and integrate with the system journal, making systemd a powerful and flexible tool for service management.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxServiceSystem Administrationinitsystemdsystemctl
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.