Linux··4 min

systemd and Services

systemd is the init system on most modern Linux distributions. It manages everything that runs on your system — services, timers, mounts and more. Understanding it is not optional for serious Linux work.

systemd is the init system used by the vast majority of modern Linux distributions. It is the first process that starts when your system boots (PID 1) and it is responsible for starting, stopping, and managing everything else — services, mounts, timers, sockets, and more.

Understanding systemd is not optional for serious Linux work. It is how you control what runs on your system.

Units

Everything in systemd is a unit. Units are configuration files that describe something systemd should manage. The most common types are:

  • .service — a background service (nginx, sshd, postgresql)
  • .timer — a scheduled task (like cron but managed by systemd)
  • .mount — a filesystem mount point
  • .socket — a network socket that activates a service on demand
  • .target — a group of units (like a runlevel)

Unit files live in:

  • /lib/systemd/system/ — units provided by packages
  • /etc/systemd/system/ — units you create or override

systemctl

systemctl is the main tool for interacting with systemd.

Starting and stopping services

sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx    # reload config without full restart

Enabling and disabling services

Starting a service only runs it now. Enabling it makes it start automatically on boot.

sudo systemctl enable nginx
sudo systemctl disable nginx

Enable and start at the same time:

sudo systemctl enable --now nginx

Checking service status

systemctl status nginx

This shows whether the service is running, its PID, recent log output, and whether it is enabled. It is usually the first thing to run when a service is misbehaving.

● nginx.service - A high performance web server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled)
     Active: active (running) since Fri 2026-05-02 09:00:00 UTC; 3h ago
    Process: 1234 ExecStartPre=/usr/sbin/nginx -t
   Main PID: 1235 (nginx)

Listing all services

systemctl list-units --type=service

Filter to only failed units:

systemctl list-units --state=failed

Writing a service unit file

This is where systemd gets powerful. You can define your own services.

Create a file at /etc/systemd/system/myapp.service:

[Unit]
Description=My Application
After=network.target
 
[Service]
Type=simple
User=jan
WorkingDirectory=/home/jan/myapp
ExecStart=/home/jan/myapp/start.sh
Restart=on-failure
RestartSec=5
 
[Install]
WantedBy=multi-user.target

Breaking this down:

  • After=network.target — do not start until the network is up
  • Type=simple — the process started by ExecStart is the main process
  • User=jan — run as this user, not root
  • Restart=on-failure — restart automatically if it crashes
  • RestartSec=5 — wait 5 seconds before restarting
  • WantedBy=multi-user.target — start this service when the system reaches normal multi-user mode

After creating the file, reload systemd so it picks it up:

sudo systemctl daemon-reload
sudo systemctl enable --now myapp

journalctl

systemd collects logs from all services into a central journal. journalctl is how you read it.

View all logs (most recent last):

journalctl

Follow logs in real time (like tail -f):

journalctl -f

Logs for a specific service:

journalctl -u nginx

Logs since the last boot:

journalctl -b

Logs from a specific time period:

journalctl --since "2026-05-01 09:00:00" --until "2026-05-01 10:00:00"

Show only errors:

journalctl -p err

Priority levels from lowest to highest: debug, info, notice, warning, err, crit, alert, emerg.

systemd timers

Timers are systemd's replacement for cron. They are more flexible and their logs go into the journal like any other service.

A timer requires two unit files — a .timer and a matching .service.

/etc/systemd/system/backup.service:

[Unit]
Description=Run backup script
 
[Service]
Type=oneshot
ExecStart=/home/jan/scripts/backup.sh
User=jan

/etc/systemd/system/backup.timer:

[Unit]
Description=Run backup daily
 
[Timer]
OnCalendar=daily
Persistent=true
 
[Install]
WantedBy=timers.target

OnCalendar=daily runs at midnight. Persistent=true means if the system was off at midnight, run the job when it next starts.

Enable and start the timer:

sudo systemctl enable --now backup.timer

List all active timers:

systemctl list-timers

systemd is comprehensive and the learning curve is real. But once you understand units, systemctl, and journalctl, you have a complete picture of what is running on your system, why, and what it is logging. That is a significant capability.

Next up: disk management — partitions, filesystems, mounting, and fstab.

0 views