BM

Systemd Services

Create a Systemd service

Overview

Systemd is the standard init system and system manager for Linux, serving as the parent of all other processes (PID 1). It is designed to provide a faster, more efficient way to start services through parallelization and to manage system resources throughout the runtime. The Init Process

  • PID 1: systemd is the first process to start after the kernel finishes loading (following the bootloader/GRUB).
  • Parallelization: Unlike the older SysVinit, which started services one-by-one, systemd uses socket and D-Bus activation to start services in parallel, significantly reducing boot times.

systemd

Process 1 -> Responsible to init all other process' Will take over after GRUB Used to manage which process's are started up

Service files .service files, primarily reside in /etc/systemd/system/.

Unit Files: The Backend Developer's Focus A typical Backend Service structure:

[Unit]
Description=My Go Backend API
After=network.target postgresql.service

[Service]
ExecStart=/usr/local/bin/my-app
Restart=always
User=www-data
Environment=APP_PORT=8080

[Install]
WantedBy=multi-user.target

systemctl

The primary command-line tool for controlling the state of the system and individual services (units) at runtime. Common Commands:

systemctl start [unit] / systemctl stop [unit]
systemctl enable [unit]: Configures the service to start automatically on boot.
systemctl status [unit]: Displays runtime information and recent log lines.
systemctl daemon-reload: Re-runs all generators and reloads unit files (essential after making manual changes to a .service file).

Systemctl StatusSystemctl Status

timedatectl

timedatectl: Used to query and change the system clock and its settings.

systemd-timesyncd
  • systemd-timesyncd: A lightweight daemon that implements an SNTP (Simple Network Time Protocol) client to synchronize the local system clock with a remote NTP server.
  • Provides a service (systemd-timesyncd.service) to allow automatic time sync
  • SNTP (Simple Network Time Protocol) implementation

localectl

Manages the system locale and keyboard layout settings, ensuring consistency between the virtual console and the X11 graphical interface.

journalctl

The query tool for systemd-journald, the centralized logging service. Why it matters: It collects data from the kernel, stdout/stderr of services, and syslog. Useful Flags:

journalctl -u [unit] -f: Follow logs for a specific service in real-time.
journalctl -p err..emerg: View only error-level logs and higher.