Service Management with systemd: Units, Logs, and Auto-Restart
You will manage existing services and author a custom systemd unit that restarts automatically on failure. By the end you will have a resilient daemon with logs in journald and a clean enable-on-boot configuration.
Learning Objectives
- Start, stop, enable, and inspect services with systemctl.
- Write a custom unit with automatic restart.
- Read service logs with journalctl.
- Time: ~2 hours · Difficulty: Intermediate · Prereqs: a systemd-based Linux server.
Architecture Overview
graph LR
Unit[/etc/systemd/system/app.service] -->|systemctl enable| SD[systemd]
SD -->|start + watch| Proc[Daemon process]
Proc -->|crash| SD
SD -->|Restart=on-failure| Proc
Proc -->|stdout/stderr| J[journald]
Environment Setup
You will need: a Linux server with systemd and a simple program or script to run as a service.
Before you begin: confirm systemd is the init system with systemctl --version.
Step-by-Step Execution
01
Inspect an existing service
Understanding state and dependencies is the basis of all service management.
systemctl status ssh --no-pager && systemctl list-dependencies ssh02
Author a custom unit with auto-restart
The Restart directive makes the daemon self-heal after a crash, key for reliability.
printf '[Unit]\nDescription=My App\n[Service]\nExecStart=/usr/local/bin/app\nRestart=on-failure\nRestartSec=3\n[Install]\nWantedBy=multi-user.target\n' | sudo tee /etc/systemd/system/app.service[ROOT REQUIRED] Creates a restart-on-failure service unit.
03
Reload, enable, and start
sudo systemctl daemon-reload && sudo systemctl enable --now app.service04
Read the logs
# journalctl -u app.service -n 5 --no-pager
Jun 17 09:02 srv01 systemd[1]: Started My App.
Jun 17 09:02 srv01 app[5123]: listening on :8080
Progress So Far
graph LR
A[01 Inspect] -->|done| B[02 Write unit]
B -->|done| C[03 Enable + start]
C -->|done| D[04 Read logs]
style A fill:#1a4a1a,stroke:#00ff00,color:#fff
style B fill:#1a4a1a,stroke:#00ff00,color:#fff
style C fill:#1a4a1a,stroke:#00ff00,color:#fff
style D fill:#1a4a1a,stroke:#00ff00,color:#fff
Testing & Validation
sudo kill -9 $(systemctl show -p MainPID --value app.service); sleep 4; systemctl is-active app.serviceAfter killing the process you should see active again within seconds, proving auto-restart works.
Troubleshooting
- Unit not found: run
systemctl daemon-reloadafter creating the file. - Service flaps: add
StartLimitIntervalSec/StartLimitBurstto avoid restart loops. - No logs: ensure the program writes to stdout/stderr, which journald captures.
Extension Ideas
- Pair the service with a systemd timer for scheduled runs.
- Sandbox it with directives like
ProtectSystem=strict. - Forward journald to a central log host (see Log Management).
Key Results
- Authored a custom unit that auto-restarts within ~3 seconds of failure.
- Enabled the service to start on boot reliably.
- Captured all service output in journald.
- Proved self-healing with a kill-and-recover test.