basics of systemd

systemd is the system and service manager for modern Linux operating systems. It handles initialization, starts services, and manages system resources efficiently.

What systemd does

systemd replaces older init systems like SysVinit. It boots the system, launches background services (like web servers or databases), and keeps everything running smoothly.

Service Management

Services are programs that run in the background. systemd calls these “units” and manages them with simple commands:

Files: Service definitions live in .service files (e.g., /etc/systemd/system/myapp.service).

Commands (using systemctl):

Action Command Example
Start systemctl start name systemctl start nginx
Stop systemctl stop name systemctl stop nginx
Restart systemctl restart name systemctl restart nginx
Enable (auto-start on boot) systemctl enable name systemctl enable nginx
Disable systemctl disable name systemctl disable nginx
Status systemctl status name systemctl status nginx

Dependency Management

Services often need others to run first (e.g., a web app needs a database). Systemd handles this automatically in unit files:

[Unit]
Description=My Web App
After=postgresql.service
Requires=postgresql.service

After: Starts this after another service.

Requires: Fails if the dependency fails.

Wants: Starts the dependency if possible (softer than Requires).

Logging with Journald

Systemd’s journald collects logs from everywhere—no more scattered /var/log files.

View logs: journalctl

Service logs: journalctl -u nginx.service

Recent logs: journalctl -f (follow live)

Last boot: journalctl -b

Example output shows timestamps, service names, and messages—search with grep or filters like –since yesterday.

Quick Start Example

Create /etc/systemd/system/hello.service:

[Unit]
Description=Hello World Service

[Service]
ExecStart=/bin/echo "Hello from systemd!"
Type=oneshot

[Install]
WantedBy=multi-user.target

Then: systemctl daemon-reload && systemctl start hello && systemctl enable hello.

Systemd keeps Linux simple and reliable—master these basics, and you’ll manage any server effortlessly.