systemd Services Explained: Run Your App on Boot the Right Way

Most Linux servers run the same small set of background programs day after day: a web server, a database, a queue worker, a small API. When the server reboots, those programs need to come back on their own. That is the job systemd handles, and this guide gives you systemd services explained in plain steps, with a working unit file you can adapt to your own app.

A systemd service is a short text file that tells systemd what to run, which user should run it, when it should start, and what to do when it stops. You write the file once, turn it on, and systemd takes care of the rest. If you want to run app on boot systemd is the tool most modern Linux distributions already use, so you do not need a separate process manager for a single app.

This guide walks through the parts of a unit file, a complete example for a Node.js app, the commands to start and stop it, and the mistakes that cause the most failed starts. It assumes a Debian or Ubuntu server and a user account with sudo access. If you are setting up a fresh machine first, read our guide on how to set up a secure Ubuntu 24.04 VPS from scratch before you deploy anything to it.

systemd services explained: what a service actually does

systemd is the first process that starts when a Linux system boots. It reads unit files, starts services in the right order, watches them while they run, and restarts them when they fail. A service unit is one type of unit file. Others handle timers, sockets, mounts, and targets, but services cover most app hosting work.

When you run an app by hand in a terminal, the app dies when you close the session or when the server reboots. A systemd service fixes both problems. It starts the app in the background, detaches it from your login session, sends its output to the system journal, and brings it back after a reboot or a crash.

How systemd decides when to start your app

Every unit file has an Install section that names a target, usually multi-user.target. That target is the normal state of a server after boot, with networking up and login available. When you enable a service, systemd creates a link that ties your service to that target. On the next boot, systemd reaches the target and starts your service with it.

The Unit section controls ordering. The line After=network.target tells systemd to wait until networking is ready before it starts your app. An API that binds to a port or a worker that calls an external service should wait for the network. A static file server can start earlier, but waiting costs almost nothing and avoids a common race.

Anatomy of a systemd unit file

A unit file has three sections. The Unit section holds the description and ordering rules. The Service section holds the command and the runtime rules. The Install section holds the target that pulls the service in at boot.

Here is a systemd unit file example for a Node.js API that listens on port 3000. It runs as a dedicated user, restarts on failure, and waits five seconds between restarts so a broken config does not spin in a tight loop.

[Unit]
Description=My Node.js API
After=network.target
Wants=network.target

[Service]
Type=simple
User=appuser
Group=appuser
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/node /opt/myapp/server.js
Restart=on-failure
RestartSec=5
Environment=NODE_ENV=production
Environment=PORT=3000

[Install]
WantedBy=multi-user.target

Each line earns its place. Type=simple tells systemd the process started by ExecStart is the main process. User and Group stop the app from running as root, which limits the damage if the app has a bug. WorkingDirectory sets the folder where relative paths resolve, so file reads and logs land where you expect. Environment sets variables without baking them into the command line.

Write and enable your first service

Unit files for your own services live in /etc/systemd/system/. Create the file with your editor, then reload systemd so it reads the new file. Reloading does not restart running services. It only makes systemd aware of new files.

sudo nano /etc/systemd/system/myapp.service
sudo systemctl daemon-reload
sudo systemctl enable myapp.service
sudo systemctl start myapp.service

The command systemctl enable service linux admins run most often is the one that wires the service into boot. Enable creates the link to multi-user.target. Start runs the service right now. You need both. Enable without start leaves the app stopped until the next reboot. Start without enable leaves the app running now but dead after the next reboot.

sudo systemctl status myapp.service
sudo systemctl is-enabled myapp.service
sudo systemctl is-active myapp.service

If status shows a failure, read the log before you change anything. The journal keeps the output of the service, including startup errors that never reach a terminal.

sudo journalctl -u myapp.service -n 50 --no-pager
sudo journalctl -u myapp.service -f

The first command prints the last 50 lines. The second follows new lines live, which helps when you watch an app start and fail in real time.

Restart rules and what they cost

Restart=on-failure restarts the service only when it exits with an error. Restart=always restarts it even when it exits cleanly, which can hide a bug that makes your app exit on purpose. For a web API or a worker, on-failure is the right default. Use always only when you know the process should never exit at all.

RestartSec sets the pause between attempts. Five seconds is a sensible default. A one-second pause with a broken config can fill the journal with thousands of identical errors in minutes.

Common mistakes that break a service

Most failed services trace back to a short list of causes. Check these first before you rewrite the unit file.

  • Wrong path in ExecStart. Use the full path to the binary. systemd does not read your shell PATH the way an interactive session does, so node or python3 alone can fail even when the command works in your terminal.
  • Missing WorkingDirectory. An app that reads a config file by relative path starts in the wrong folder and cannot find it. Set WorkingDirectory to the app folder.
  • Permission errors. The app user needs read access to the code and write access to any log or upload folder. Run ls -l on those paths and fix ownership with chown before you blame the unit file.
  • Environment variables missing. Variables set in your shell do not carry into the service. Put them in Environment lines or an EnvironmentFile.

When a service fails in a loop, stop it first so the log stops growing, then read the journal from the start of the failure. The first error line usually names the real cause. Later lines are often side effects of the same failure.

Monitor a service after it goes live

Starting a service is not the end of the job. Check its memory use and restart count over the first week. A service that restarts every hour has a bug, a memory leak, or a dependency that drops. A service that never restarts and serves traffic is doing its job.

sudo systemctl show myapp.service -p NRestarts
sudo systemctl show myapp.service -p MemoryCurrent

If the server feels slow after you add services, read how to diagnose high server load to find the hungry process before you tune systemd.

Pair the service with basic monitoring so you hear about a failure before a user reports it. A simple uptime check plus a log alert covers most small servers. Our Linux server monitoring basics guide explains which numbers to watch and which tools collect them without much setup.

FAQ

Where do I put a custom service file?

Put it in /etc/systemd/system/ with a .service extension. Files in /lib/systemd/system/ belong to packages installed by your distribution, and package updates can overwrite them.

How do I update a service after editing the file?

Run sudo systemctl daemon-reload, then sudo systemctl restart myapp.service. The reload makes systemd read the new file. The restart applies it to the running process.

Why does my service work by hand but fail under systemd?

The usual causes are a missing full path, a missing working directory, or environment variables that exist in your shell but not in the service. Compare the two environments line by line, starting with the journal error.

Write the unit file, enable it, start it, and check the journal. Once those four steps work for one app, the same pattern covers every app you host on that server.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.