Notes
Unit files
systemd manages units. A service unit runs a program, a socket unit starts something when a connection arrives, a timer unit runs things on a schedule, a mount unit mounts a filesystem, and a target groups other units. Units are small text files. Yours go in etc systemd system; the ones installed by packages live in lib systemd system, and yours win if the names match.
- .service a program to run
- .socket start on connection
- .timer run on a schedule
- .mount a filesystem
- .target a group of units
- yours: /etc/systemd/system/ packages: /lib/systemd/system/
ShopRung's unit
Here is ShopRung's unit. The unit section gives a description and says start after the network is up. The service section says run as the user shoprung, never root, in the folder opt shoprung, with this exact command and this environment variable. Restart on failure, after two seconds. The install section says this service belongs to multi user target, so enabling it makes it start at boot. Thirteen lines, and the app is now a real service.
[Unit] Description=ShopRung After=network-online.target Wants=network-online.target [Service] User=shoprung WorkingDirectory=/opt/shoprung ExecStart=/usr/bin/node src/server.js Environment=PORT=8080 Restart=on-failure RestartSec=2 [Install] WantedBy=multi-user.target
Verify first
Before starting it, check it. systemd analyze verify reads the unit and reports problems, and on our machine it found one: the command slash usr slash bin slash node did not exist, because Node was installed in opt node twenty two. ExecStart needs a full, real path; systemd does not use your shell's PATH. which node gives the right one. Thirty seconds of verify saves a confusing failed start later.
$ systemd-analyze verify
shoprung.service
shoprung.service: Command
/usr/bin/node is not
executable: No such file
or directory
on that machine node lived in
/opt/node22/bin/node
fix: ExecStart with the real
path (which node) systemctl
After creating or changing a unit, run daemon reload so systemd reads it again. Start runs it now. Enable makes it start at boot; enable with now does both. Status shows whether it is active, its main process ID, memory, and the last few log lines, which is the first thing to check when a service misbehaves. Restart restarts it, and list units with failed shows every service that has crashed.
$ sudo systemctl daemon-reload $ sudo systemctl start shoprung $ sudo systemctl enable shoprung (or enable --now: both) $ systemctl status shoprung ● Active: active (running) Main PID, memory, last logs $ sudo systemctl restart shoprung $ systemctl list-units --failed
Restarts
With restart on failure, if ShopRung exits with an error or is killed, systemd waits two seconds and starts it again. If it crashes too many times in a short window, systemd gives up and marks it failed, so a broken app does not spin forever. When you stop a service, systemd sends SIGTERM, waits up to ninety seconds by default, then SIGKILL, exactly as in the last episode. Remember that automatic restarts hide crashes, so always read the logs.
- exit code ≠ 0 or killed → wait RestartSec, start again
- too many restarts in a short window → systemd gives up (StartLimitBurst, Interval)
- stop: SIGTERM, wait 90 s (TimeoutStopSec), then KILL
- a restart loop hides the bug: read the logs
journald
Anything a service prints to standard output or error is captured by journald, tagged with the unit name, and indexed. journalctl minus u shoprung shows ShopRung's logs; minus f follows them live. Since ten minutes ago narrows the time. Minus b with priority error shows only errors since the last boot. And disk usage tells you how much space the journal uses, which you can cap in journald's config.
$ journalctl -u shoprung
$ journalctl -u shoprung -f
follow, like tail -f
$ journalctl -u shoprung
--since "10 min ago"
$ journalctl -b -p err
errors since this boot
$ journalctl --disk-usage Overrides
To change a service that came with a package, never edit its file, because updates overwrite it. systemctl edit creates a drop-in override file, where you add only the lines you want to change, like a higher open-files limit or a different restart policy. systemctl cat shows the original file and every override together, so you can see exactly what systemd will run.
$ sudo systemctl edit nginx creates /etc/systemd/system/ nginx.service.d/override.conf [Service] LimitNOFILE=65536 Restart=always $ systemctl cat nginx shows the file + overrides
Hardening for free
systemd can also sandbox a service with a few lines. No new privileges, protect system strict and protect home make most of the disk read-only or invisible to it. Private tmp gives it its own temporary folder, and read write paths opens just the folders it needs. Memory max and CPU quota set resource limits using cgroups, the same mechanism containers use. systemd analyze security scores how exposed a service is.
- NoNewPrivileges=true
- ProtectSystem=strict
- ProtectHome=true
- PrivateTmp=true
- ReadWritePaths=/var/lib/shoprung
- MemoryMax=512M (cgroup limit)
- CPUQuota=50%
- $ systemd-analyze security shoprung
Common mistakes
Mistakes. Changing a unit and forgetting daemon reload. Starting a service but never enabling it, so it vanishes after reboot. A relative or wrong ExecStart path. Running the service as root. Editing package unit files instead of using overrides. And a restart policy that hides a crash loop because nobody reads the journal.
- forgetting daemon-reload
- start but never enable
- relative ExecStart or relying on your shell's PATH
- running services as root
- editing /lib/systemd files
- Restart=always hiding a crash loop
Say it like this
In the interview: I write a unit file in etc systemd system with a dedicated user, a working directory, the full ExecStart path, restart on failure and wanted by multi user target. I check it with systemd analyze verify, run daemon reload, then enable with now, and check status. Logs come from journalctl minus u, and I change packaged services with drop-in overrides, adding sandboxing options and limits.
- "I write a unit in /etc/systemd/ system: its own user, working
- directory, full ExecStart path, Restart=on-failure, WantedBy= multi-user.target. Verify, daemon- reload, enable --now, check
- status, and read logs with journalctl -u."