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.
[Unit]Description=Payment Processing APIAfter=network.target postgresql.serviceRequires=postgresql.serviceWants=redis.service# Allow at most 3 start attempts within 60 secondsStartLimitIntervalSec=60StartLimitBurst=3 [Service]User=payment-svcGroup=payment-svcWorkingDirectory=/opt/payment-serviceExecStart=/usr/bin/node /opt/payment-service/index.jsExecReload=/bin/kill -HUP $MAINPID# Secrets come from a restricted file, not from this unitEnvironmentFile=/etc/payment-service/envEnvironment=NODE_ENV=productionEnvironment=PORT=4000Restart=on-failureRestartSec=5# simple: ExecStart process is the service (most common)Type=simpleLimitNOFILE=65536 [Install]WantedBy=multi-user.targetBy default a service receives SIGTERM on stop, so ExecStop= is only needed for special cases.
Choosing a Service Type
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.
sudo systemctl edit nginx# Or by hand:sudo mkdir -p /etc/systemd/system/nginx.service.dsudo tee /etc/systemd/system/nginx.service.d/override.conf <<'EOF'[Service]LimitNOFILE=65536Restart=alwaysRestartSec=3EOFsudo systemctl daemon-reloadsudo systemctl restart nginxPractical Unit File Commands
systemctl cat nginx # unit plus overridessystemd-analyze verify /etc/systemd/system/myapp.servicesystemctl show -p FragmentPath nginxsystemctl list-unit-files --type=servicesudo systemctl daemon-reload # after any editTroubleshooting 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 MistakeOmitting
WantedBy=multi-user.targetin [Install].systemctl enablethen has nothing to link and the service does not start at boot.
SecurityNever put passwords or API keys directly in a unit file, because
systemctl catshows it. UseEnvironmentFile=pointing to a file with restricted permissions such as 640, or a secrets manager.
TipRun
systemd-analyze verifybefore 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.