Build a service

Write a minimal service

Create a small unit with an explicit executable, working directory, user, and restart boundary.

8 minute lesson

~~~

A useful service unit states exactly what process systemd should run and which account should own it.

Use absolute paths in ExecStart=. Set WorkingDirectory= only when the program needs it. Run network applications as a dedicated unprivileged user rather than root.

Create a disposable hello.service that runs /usr/bin/sleep infinity as a regular service account. Validate it with systemd-analyze verify before starting it.

Create /etc/systemd/system/hello.service:

[Unit]
Description=Practice HTTP service

[Service]
ExecStart=/usr/bin/node /opt/hello/server.js
User=hello
Restart=on-failure

[Install]
WantedBy=multi-user.target

Run systemd-analyze verify on the file before daemon-reload. Start it without enabling it first, inspect status and logs, then stop it. Use an absolute executable path and a dedicated user so the unit does not depend on an interactive shell. Finally, inspect the effective unit with systemctl cat hello.service and confirm it matches the file you intended to load.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →