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

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 ssh
02
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.service
04
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

Testing & Validation

sudo kill -9 $(systemctl show -p MainPID --value app.service); sleep 4; systemctl is-active app.service

After killing the process you should see active again within seconds, proving auto-restart works.

Troubleshooting
  • Unit not found: run systemctl daemon-reload after creating the file.
  • Service flaps: add StartLimitIntervalSec/StartLimitBurst to 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.