Writing service units and drop-ins
Quick answer
A small service unit describes what executable runs, under which identity, and how the manager treats completion. A drop-in changes selected settings without editing vendor files.
Prepare the practice environment
Run python3 lab-fixtures/boot-services/setup.py from the extracted package root as an ordinary user in a disposable Linux environment. Enter the printed path and check pwd. Use a fresh fixture and a fresh Bash process for each offline example, with LC_ALL=C and set -o pipefail. Do not omit the explicit offline root. Read the fixture README before VM-only examples; never run those on the host. Static checks do not start a service.
How it works
The supplied bytemind-demo.service is a harmless oneshot running /usr/bin/true as a dedicated account bm-demo. Type=oneshot waits for completion; RemainAfterExit=yes keeps the successful unit logically active afterward without a running process. User= limits the identity, not all capabilities by itself; NoNewPrivileges=yes is an additional defense, not a complete sandbox. ExecStart= is parsed by systemd, not by an implicit shell. A drop-in in unit.service.d/*.conf can set TimeoutStartSec; replacing a list-valued ExecStart generally requires an empty ExecStart= before a new value. The private fixture uses an unprivileged user for offline checks. The optional VM installation requires a fresh dedicated account, unique names, snapshot and console. Do not install on a real host.
A service does not automatically inherit your terminal working directory, aliases or shell startup files. Use WorkingDirectory= if a task needs a specific directory and specify its executable and arguments explicitly. For a system service the default working directory is normally the root directory. A successful systemd-analyze verify does not prove that the configured identity can read inputs or write outputs; validate those permissions during the authorized VM run.
Try the examples
The named user must exist before a real start. The file is not installed.
Expected stdout:
[Unit]Description=ByteMind finite demo[Service]Type=oneshotUser=bm-demoExecStart=/usr/bin/trueRemainAfterExit=yesNoNewPrivileges=yes[Install]WantedBy=multi-user.targetsystemd-analyze verify ./bytemind-demo.service && printf 'unit verified\n'cat bytemind-demo.service.d/10-timeout.confThis verifies parsing, not that bm-demo can authenticate or run a service.
Expected stdout:
unit verified[Service]TimeoutStartSec=10s# VM ONLY: after checking names are unused and taking a snapshotsudo useradd --system --no-create-home --shell /usr/sbin/nologin bm-demosudo install -m 0644 bytemind-demo.service /etc/systemd/system/bytemind-demo.servicesudo install -d -m 0755 /etc/systemd/system/bytemind-demo.service.dsudo install -m 0644 bytemind-demo.service.d/10-timeout.conf /etc/systemd/system/bytemind-demo.service.d/10-timeout.confsudo systemctl daemon-reloadsudo systemctl start bytemind-demo.servicesystemctl status bytemind-demo.service --no-pagerOnly after reviewing the VM lab guide; expect active (exited), subject to local policy. Stop the sample and restore the snapshot after practice.
Manual example; not executed locally. Follow its stated environment restrictions.
Common mistakes
Do not run a service as root merely to fix a permission error. Do not assume shell operators in ExecStart work automatically. Never edit vendor unit files just to set one override.
Practice
Explain the identity, executable, timeout and completion state in the supplied unit and drop-in. Optionally install only in the authorized snapshot VM and restore it afterward.
Related references
Study: systemd-analyze, systemctl. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Writing service units and drop-ins — Knowledge Check
Apply boot and systemd concepts to offline unit files and VM-only scenarios.
What does User= specify?
What does RemainAfterExit=yes allow for a successful oneshot?
Where should a small override normally live?
Does ExecStart automatically invoke a shell?