Skip to main content

Unit File

A systemd unit file is a configuration file describing a service, timer, socket, or other resource that systemd manages. A service unit has up to three main sections: [Unit] for description and dependencies, [Service] for how to run it, and [Install] for boot behavior.

Understanding systemd Unit Files

What Is a systemd Unit File?

A unit file is a recipe card for systemd. It states what to run, as which user, what it depends on, how to restart it after a crash, and whether to start it at boot. Before systemd this took a long shell script with PID files and restart logic; a unit file does it in a few declarative lines.

Anatomy of a Service Unit File

Save this as /etc/systemd/system/payment-service.service. Comments must be on their own lines, because systemd treats text after a value on the same line as part of that value.

INI
[Unit]
Description=Payment Processing API
After=network.target postgresql.service
Requires=postgresql.service
Wants=redis.service
# Allow at most 3 start attempts within 60 seconds
StartLimitIntervalSec=60
StartLimitBurst=3
[Service]
User=payment-svc
Group=payment-svc
WorkingDirectory=/opt/payment-service
ExecStart=/usr/bin/node /opt/payment-service/index.js
ExecReload=/bin/kill -HUP $MAINPID
# Secrets come from a restricted file, not from this unit
EnvironmentFile=/etc/payment-service/env
Environment=NODE_ENV=production
Environment=PORT=4000
Restart=on-failure
RestartSec=5
# simple: ExecStart process is the service (most common)
Type=simple
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target

By default a service receives SIGTERM on stop, so ExecStop= is only needed for special cases.

Choosing a Service Type

TEXT
simple ExecStart process is the service (default for most apps)
forking the process forks and the parent exits (older daemons)
oneshot runs once and exits (setup tasks)
notify the service tells systemd when it is ready (sd_notify)

How to Override a Packaged Unit Safely

Never edit files under /usr/lib/systemd/system/. Add a drop-in override that survives package upgrades.

Bash
sudo systemctl edit nginx
# Or by hand:
sudo mkdir -p /etc/systemd/system/nginx.service.d
sudo tee /etc/systemd/system/nginx.service.d/override.conf <<'EOF'
[Service]
LimitNOFILE=65536
Restart=always
RestartSec=3
EOF
sudo systemctl daemon-reload
sudo systemctl restart nginx

Practical Unit File Commands

Bash
systemctl cat nginx # unit plus overrides
systemd-analyze verify /etc/systemd/system/myapp.service
systemctl show -p FragmentPath nginx
systemctl list-unit-files --type=service
sudo systemctl daemon-reload # after any edit

Troubleshooting Unit Files

Symptom Command What to Look For
Change ignored sudo systemctl daemon-reload Reload needed
Exits immediately journalctl -u name -n 20 Exit code and error
Wrong user systemctl show name -p User User= directive
Env not loading systemctl show name -p EnvironmentFiles Path exists
Common Mistake

Omitting WantedBy=multi-user.target in [Install]. systemctl enable then has nothing to link and the service does not start at boot.

Security

Never put passwords or API keys directly in a unit file, because systemctl cat shows it. Use EnvironmentFile= pointing to a file with restricted permissions such as 640, or a secrets manager.

Tip

Run systemd-analyze verify before reloading. It catches syntax errors that would otherwise make a service fail to load.

Frequently Asked Questions

What does the [Service] section control beyond the start command?

It sets the run-as user, restart policy (`Restart=`, `RestartSec=`), process type, environment, and resource limits such as `LimitNOFILE`, `MemoryMax`, and `CPUQuota`. A cron entry or plain script offers none of this supervision.

Why does my enabled service not start on boot?

The most common cause is a missing `WantedBy=multi-user.target` in [Install], so enabling creates no link. Check `systemctl is-enabled name` and reload after fixing the file.

Why does my unit file change have no effect?

systemd caches unit definitions. Run `sudo systemctl daemon-reload` and restart the service so it uses the new file.

Found a mistake, an outdated step or a broken link?