Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Where Unit Files Live

FolderUsed for
/usr/lib/systemd/systemFiles installed by packages. Do not edit.
/etc/systemd/systemYour own units and overrides. These win.
/run/systemd/systemUnits 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= and Requires= 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.target means "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. Use forking only 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), or always. Pair it with RestartSec=.

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

  1. Check the file: sudo systemd-analyze verify /etc/systemd/system/myapp.service.
  2. Make systemd read it: sudo systemctl daemon-reload.
  3. Start it now and at every boot: sudo systemctl enable --now myapp.
  4. Check it: systemctl status myapp and journalctl -u myapp -f.
  5. 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-reload was not run.
  • Exits right away with status 203: the path in ExecStart is 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.

Was this answer helpful?
Loading...