Home/Learn/Linux/systemd — Service Management

systemd — Service Management

Intermediate
Services

systemd is the init system and service manager on modern Linux distros. systemctl, journalctl, and unit files let you start, stop, enable, and inspect any service on the system.

Overview

systemd replaced SysV init as the Linux init system (PID 1). It starts all other services in parallel at boot and manages their lifecycle. Services are defined in unit files (.service). systemctl is the primary interface: start/stop/restart services, enable/disable at boot, check status. journalctl queries the systemd journal — the unified log for all services. Writing your own .service unit file is the standard way to deploy a daemon application on a Linux server without Docker.

systemctl — Service Control

systemctl is the one command that covers service management, system state, and boot configuration.

bash — systemctl service management
# Service lifecycle
systemctl start nginx          # start now
systemctl stop nginx           # stop now
systemctl restart nginx        # stop then start
systemctl reload nginx         # reload config without full restart (if supported)
systemctl enable nginx         # start at boot
systemctl disable nginx        # don't start at boot
systemctl enable --now nginx   # enable + start in one command

# Status and inspection
systemctl status nginx
# ● nginx.service - A high performance web server
#      Loaded: loaded (/lib/systemd/system/nginx.service; enabled)
#      Active: active (running) since Mon 2024-01-15 10:00:00 UTC; 2h 5min ago
#    Main PID: 1234 (nginx)
#     CGroup: /system.slice/nginx.service
#             ├─1234 nginx: master process
#             â””─1235 nginx: worker process

# Is the service active/enabled?
systemctl is-active nginx      # "active" or "inactive"
systemctl is-enabled nginx     # "enabled" or "disabled"

# System-wide
systemctl list-units --type=service          # all loaded services
systemctl list-units --type=service --failed # failed services
systemctl list-unit-files --type=service     # all installed service files

# Boot analysis
systemd-analyze time          # total boot time
systemd-analyze blame         # which services took longest to start
systemd-analyze critical-chain nginx  # dependency chain for nginx

journalctl — Logs

journalctl queries the systemd journal — the unified log store for all services managed by systemd.

bash — journalctl log querying
# Basic log queries
journalctl                            # all logs (oldest first, paged)
journalctl -r                         # reverse (newest first)
journalctl -f                         # follow (like tail -f)
journalctl -n 100                     # last 100 lines

# Filter by service
journalctl -u nginx                   # nginx logs only
journalctl -u nginx -f                # follow nginx logs
journalctl -u nginx --since "1 hour ago"
journalctl -u nginx --since "2024-01-15" --until "2024-01-16"

# Filter by priority
journalctl -p err                     # errors and above
journalctl -p warning -u nginx        # warnings+ for nginx

# Boot logs
journalctl -b                         # logs from current boot
journalctl -b -1                      # logs from previous boot
journalctl -b --list-boots            # list all boots

# Follow multiple services
journalctl -u nginx -u postgresql -f

# Disk usage and rotation
journalctl --disk-usage               # how much disk logs use
journalctl --vacuum-size=500M         # keep only 500 MB of logs
journalctl --vacuum-time=30d          # keep only 30 days of logs

Writing a .service Unit File

A .service file defines how systemd manages your application — how to start it, what user to run as, environment variables, restart policy, and dependencies.

systemd .service unit file
# /etc/systemd/system/myapp.service
[Unit]
Description=My Node.js Application
After=network.target postgresql.service     # start after network and DB are up
Requires=postgresql.service                 # fail if postgres is not running

[Service]
Type=simple                     # process stays in foreground (most apps)
User=appuser                    # run as non-root service account
Group=appuser
WorkingDirectory=/opt/myapp

# Environment
Environment=NODE_ENV=production
Environment=PORT=3000
EnvironmentFile=/etc/myapp/env  # load from file (keep secrets out of unit file)

# Start and stop
ExecStart=/usr/bin/node /opt/myapp/server.js
ExecStop=/bin/kill -TERM $MAINPID
ExecReload=/bin/kill -HUP $MAINPID

# Restart policy
Restart=on-failure              # restart if exits with non-zero code
RestartSec=5                    # wait 5 seconds before restarting
StartLimitIntervalSec=60        # within 60 seconds
StartLimitBurst=3               # if it fails 3 times, stop trying

# Resource limits
LimitNOFILE=65536               # max open file descriptors
MemoryMax=512M                  # cgroup memory limit

# Security
NoNewPrivileges=true
PrivateTmp=true                 # isolated /tmp

[Install]
WantedBy=multi-user.target      # enable for normal multi-user boot

# Deploy the unit file:
# sudo cp myapp.service /etc/systemd/system/
# sudo systemctl daemon-reload     ← reload unit files after changes
# sudo systemctl enable --now myapp

Key Points to Remember

  • 1systemctl enable --now is the one command to both enable at boot and start immediately.
  • 2journalctl -u service -f follows live logs for a specific service — like tail -f but better.
  • 3After a .service file change, run systemctl daemon-reload before systemctl restart.
  • 4Restart=on-failure + RestartSec provides automatic recovery for crashed services.
  • 5EnvironmentFile keeps secrets out of the unit file (which is world-readable).
  • 6systemd-analyze blame shows which services slow down boot — essential for optimisation.

Interview Questions

Sign in to ask Aria
1

How do you deploy a Node.js application as a Linux service that starts on boot?

2

How do you check why a service failed to start?

Ask Aria about systemd — Service Management

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…