Build a service

Choose a service type

Choose Type=simple, exec, notify, forking, or oneshot from the process behavior you actually control.

8 minute lesson

~~~

A service type tells systemd two things: when startup has succeeded, and how to identify the main process. Pick it from how your program behaves, not from what you copied last time.

The types that matter

Type=simple is the default. systemd considers the service started the instant the process is forked, before your binary even runs. That means systemctl start can report success for a service whose executable does not exist.

Use Type=exec for a normal foreground process when an execution failure must fail startup. It behaves like simple, but systemctl start waits until the binary has actually been executed, so a wrong path or a permission problem fails the start command itself:

[Service]
Type=exec
ExecStart=/usr/bin/node /opt/app/server.js

Type=notify needs application support: the program calls sd_notify() to send READY=1 when it can really serve traffic. That makes ordering meaningful for anything started After= this service. Only use it when the application documents that support.

Type=forking exists for traditional daemons that detach: the parent exits, a child keeps running. Pair it with PIDFile= so systemd can track the right process. New software should stay in the foreground instead.

Type=oneshot runs finite setup work. systemd waits for the process to exit before considering the unit started. Add RemainAfterExit=yes when the unit should stay active (exited) after finishing:

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/prepare-cache-dirs

The failure mode to recognize

The classic mismatch is Type=forking on a program that never forks. systemd waits for the parent to exit, the parent never does, and startup hangs until the timeout kills it. The journal shows a start timeout even though the process was healthy the whole time. The fix is not a longer timeout. It is the right type.

Read the startup behavior of an application you run: does it stay in the foreground, detach, or finish and exit? Choose a type and write down the event that should count as successful startup.

Lesson completed

Take this course offline

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

Get the download library →