How systemd Unit Files Define Linux Services

In the Linux operating system, systemd manages system initialization, background daemons, and processes using declarative configuration files known as unit files. This article explains how a systemd .service unit file defines the execution behavior, resource environments, and lifecycle of a Linux service. It breaks down the core sections of a unit file—[Unit], [Service], and [Install]—and covers how these directives tell the operating system when and how to run a program.

Location of Service Unit Files

Systemd looks for service unit files in specific directories based on their priority and purpose:

Anatomy of a .service File

A service unit file is an INI-style plain text file ending with the .service extension. It is organized into three primary sections: [Unit], [Service], and [Install].

1. The [Unit] Section

The [Unit] section contains general metadata about the service and defines its relationships and startup order relative to other units.

2. The [Service] Section

The [Service] section specifies the exact configuration for process execution, environment control, and process management.

3. The [Install] Section

The [Install] section contains information used when enabling or disabling the service via systemctl enable or systemctl disable.

Example Service Configuration

Below is a practical example of a custom service file defined at /etc/systemd/system/myapp.service:

[Unit]
Description=Custom Node.js Application Service
After=network.target

[Service]
Type=simple
User=appuser
Group=appuser
WorkingDirectory=/var/www/myapp
ExecStart=/usr/bin/node /var/www/myapp/server.js
Restart=on-failure
RestartSec=5s
Environment=PORT=3000

[Install]
WantedBy=multi-user.target

Activating and Managing the Service

After creating or modifying a unit file, systemd must be told to reload its configuration cache, after which standard control commands can be used: