Linux Command Reference ยท Systemd and Logging (Read-Only)
Unit, Service and Install sections
Anatomy of a systemd service unit file: the [Unit], [Service] and [Install] sections, the common directives, service Type values and where unit files live.
A systemd service unit is an INI-style text file, usually name.service, that tells systemd how to run a program. Almost every unit has three sections. [Unit] describes the unit and its relationships, [Service] says how to start, stop and supervise the process, and [Install] says what systemctl enable should hook the unit into.
Unit files from packages live in /usr/lib/systemd/system/ (or /lib/systemd/system/); local files and overrides go in /etc/systemd/system/, which takes precedence. The sandbox has no systemd, so everything here is read-only knowledge, but a unit file is plain text and the examples parse one.
[Unit] Description=Example API After=network-online.target [Service] ExecStart=/usr/local/bin/api --port 8080 User=api Restart=on-failure [Install] WantedBy=multi-user.target
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
A unit file has [Section] headers and Key=value lines. Lines starting with # or ; are comments. A key can repeat (for list-like keys); assigning an empty value resets the list.
[Unit]
| Name | Description |
|---|---|
Description= |
Short text shown in systemctl status and logs. |
Documentation= |
URLs or man: references. |
After=, Before= |
Ordering only (see Unit dependencies). |
Requires=, Wants=, Conflicts= |
Dependency relationships. |
ConditionPathExists= and friends |
Skip the unit quietly if a condition is false. |
[Service]
| Name | Description |
|---|---|
Type= |
simple (default with ExecStart; process stays in the foreground), exec, forking (daemon forks; pair with PIDFile=), oneshot (runs and exits), notify (the program tells systemd when it is ready), idle. |
ExecStart= |
The command line. It must be an absolute path; no shell syntax such as pipes or &&. |
ExecStartPre=, ExecStartPost=, ExecStop=, ExecReload= |
Extra commands around the lifecycle. |
User=, Group=, WorkingDirectory= |
Run identity and directory. |
Environment=, EnvironmentFile= |
Variables, inline or from a file (- before the path means optional). |
Restart=, RestartSec= |
Restart policy (see Restart policies). |
TimeoutStartSec=, TimeoutStopSec= |
How long to wait before declaring failure. |
[Install]
| Name | Description |
|---|---|
WantedBy= |
Target that should pull the unit in when enabled, usually multi-user.target. |
RequiredBy=, Alias=, Also= |
Stronger link, extra names, companion units to enable together. |
Errors and edge cases: ${VAR} expands from the unit's environment but $VAR in ExecStart= is also substituted by systemd, not by a shell; use sh -c '...' when you need real shell features. A relative path in ExecStart= is an error.
Usage notes
- Run
systemctl daemon-reloadafter editing any unit file; check for mistakes withsystemd-analyze verify name.service. - Do not edit packaged files. Use
systemctl edit name.serviceto write a drop-in at/etc/systemd/system/name.service.d/override.conf. - Foreground programs only with
Type=simple. If a program daemonizes itself withforking, useType=forking; otherwise systemd thinks it exited. - Quote carefully. Percent signs are specifiers (
%n,%i,%h); write%%for a literal one. - Logs. Anything the service writes to stdout or stderr goes to the journal automatically: see journalctl filters.
- Template units such as
[email protected]are started as[email protected];%iexpands to1. - Security is opt-in. Add sandboxing directives from Service sandboxing; a default service is not confined.