Home/Engineer/Linux / Shell/Managing systemd Services: s…
Linux / ShellBeginnerubuntucentosdebiansystemdsystemctljournalctl

Managing systemd Services: systemctl Commands, Custom Unit Files, and Checking Logs with journalctl

A step-by-step guide to starting, stopping, and enabling services with systemctl, and writing your own service unit files.

Contents
  1. What is systemd?
  2. Basic Service Operations
  3. Viewing All Service Status
  4. Writing Your Own Service Unit File
  5. Checking Logs (journalctl)
  6. Timers (Cron Replacement)

What is systemd?

The standard init system on modern Linux distributions (Ubuntu 22.04+, RHEL/Rocky Linux 8+, Debian 11+). It starts services in parallel at boot and manages their dependencies.


Basic Service Operations

Bash
# Check status
systemctl status nginx

# Start / stop / restart
systemctl start  nginx
systemctl stop   nginx
systemctl restart nginx
systemctl reload nginx      # Reload configuration only (no downtime)

# Enable/disable start on boot
systemctl enable  nginx
systemctl disable nginx

# Enable and start immediately (in one step)
systemctl enable --now nginx

Viewing All Service Status

Bash
# List running services
systemctl list-units --type=service --state=running

# Check failed services
systemctl --failed

# Analyze boot time
systemd-analyze blame | head -20

Writing Your Own Service Unit File

INI
# /etc/systemd/system/myapp.service

[Unit]
Description=My Node.js Application
After=network.target

[Service]
Type=simple
User=deploy
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/node /opt/myapp/server.js
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal
Environment=NODE_ENV=production PORT=3000

[Install]
WantedBy=multi-user.target

After writing the file, apply it:

Bash
systemctl daemon-reload          # Always run this after changing a unit file
systemctl enable --now myapp

Checking Logs (journalctl)

Bash
journalctl -u nginx                    # Full logs for the nginx service
journalctl -u nginx -f                 # Follow in real time
journalctl -u nginx --since "1 hour ago"
journalctl -u nginx -n 50             # Last 50 lines
journalctl -u nginx -p err            # Errors only
journalctl --disk-usage               # Log disk usage
journalctl --vacuum-time=7d           # Delete logs older than 7 days

Timers (Cron Replacement)

INI
# /etc/systemd/system/backup.timer
[Unit]
Description=Daily Backup Timer

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target
Bash
systemctl enable --now backup.timer
systemctl list-timers
#systemd#systemctl#journalctl#linux
Editorial note

AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us

Topic-related official docs (auto-linked · not per-claim sources)Linux man pages ↗

Questions & answers

Ask a question about this guide. We'll review and reply.