Where Unit Files Live
| Folder | Used for |
|---|---|
/usr/lib/systemd/system | Files installed by packages. Do not edit. |
/etc/systemd/system | Your own units and overrides. These win. |
/run/systemd/system | Units created at runtime; gone after a reboot. |
Package updates replace files under /usr/lib/systemd/system, so changes made there are lost. To see the file systemd actually uses, including any overrides, run systemctl cat nginx.
The Three Sections
- [Unit]: the description and the order.
After=says what must start first;Wants=andRequires=pull other units in. - [Service]: how to run the program:
ExecStart=, the user, the working folder, the environment, and the restart policy. - [Install]: what happens on
systemctl enable.WantedBy=multi-user.targetmeans "start at normal boot".
Key Options in [Service]
- ExecStart: the command, with a full path. It is not run through a shell, so pipes, redirection, and
&&do not work here. - Type:
simple, the default, fits a program that stays in the foreground. Useforkingonly for an old-style daemon that puts itself in the background. - User and Group: the account the program runs as. Without them it runs as root.
- Environment and EnvironmentFile: variables for the program. Your shell profile is not read.
- Restart:
no(the default),on-failure(after a crash or non-zero exit), oralways. Pair it withRestartSec=.
A Complete Custom Service
This example runs a program in /opt/myapp as an unprivileged system user. First create the user and give it the folder:
sudo useradd --system --no-create-home --shell /usr/sbin/nologin myapp
sudo chown -R myapp:myapp /opt/myapp
Then create /etc/systemd/system/myapp.service with sudo nano:
[Unit]
Description=My application
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=myapp
Group=myapp
WorkingDirectory=/opt/myapp
EnvironmentFile=-/etc/myapp.env
ExecStart=/opt/myapp/bin/server --port 8080
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
The minus sign before the environment file path means the service still starts if that file does not exist.
Load, Start, and Enable It
- Check the file:
sudo systemd-analyze verify /etc/systemd/system/myapp.service. - Make systemd read it:
sudo systemctl daemon-reload. - Start it now and at every boot:
sudo systemctl enable --now myapp. - Check it:
systemctl status myappandjournalctl -u myapp -f. - If it does not run, fix the cause shown in the journal before you rely on it at boot.
Run daemon-reload again every time you change the file, then restart the service.
Change a Packaged Service with systemctl edit
To change a service that came from a package, do not edit its file. Create a drop-in instead:
sudo systemctl edit nginx
An editor opens. Lines you add are saved in /etc/systemd/system/nginx.service.d/override.conf and survive package updates. For example, to restart nginx after a crash:
[Service]
Restart=on-failure
RestartSec=5
systemd reloads its configuration when you save; restart the service to apply the change. To replace ExecStart, first clear it with an empty ExecStart= line, then add the new one. sudo systemctl revert nginx removes all overrides.
Common Mistakes
- Changes have no effect:
daemon-reloadwas not run. - Exits right away with status 203: the path in
ExecStartis wrong or the file is not executable. - Permission denied: the service user cannot read the folder or write its log. Fix ownership; never use
chmod 777. - Restarts in a loop: read
journalctl -u myapp; after too many restarts in a short time, systemd gives up and marks it failed.
To see all your services at a glance, read how to list services with systemctl. A service replaces jobs started with nohup, and it comes back by itself after a reboot.