Skip to content

systemd service management — start, stop, enable, debug

The essential systemctl commands every operator should have at fingertip speed, plus the few non-obvious flags that turn a frustrating debug session into a quick one.

systemd is the init and service manager on essentially every modern Linux distro. The day-to-day surface for an operator is systemctl plus a handful of file paths. This article is the quick-reference we hand to new operators on day one: the commands that come up dozens of times a day, the flags that turn a frustrating debug into a fast one, and the patterns we use for writing unit files. For the longer-form patterns (templated units, timer integration, hardening directives), the deeper article in our KB is the next stop.

How to verify

What is running, what is enabled, what failed.

systemctl list-units --type=service --state=running
systemctl list-units --type=service --state=failed
systemctl list-unit-files --state=enabled
systemctl --failed
systemctl is-active nginx postgresql
systemctl is-enabled nginx postgresql

systemctl --failed is the single most useful query when something is wrong on a host you do not know — it lists every unit in failure state.

What’s happening

A unit is a description of something systemd can manage: a service, a socket, a mount, a device, a timer. The most common is a service. Each unit has a unit file — a small INI-style description of what to run, when, with what privileges, what to do if it crashes. Distros ship unit files in /lib/systemd/system/ or /usr/lib/systemd/system/. Local overrides live in /etc/systemd/system/. Drop-in fragments under /etc/systemd/system/<unit>.d/*.conf extend or override specific settings without rewriting the whole file.

systemctl is the front end. It loads unit files, computes the dependency graph, and starts/stops/restarts units while honoring Requires, Wants, After, Before relations.

journalctl is the companion log reader — every unit’s stdout/stderr goes into journald and is queryable by unit, time range, priority, or boot. See journalctl-log-reading for the dedicated reference.

The procedure

  1. Start, stop, restart, reload.

    sudo systemctl start nginx
    sudo systemctl stop nginx
    sudo systemctl restart nginx
    sudo systemctl reload nginx       # SIGHUP, no downtime — only if the unit declares ExecReload
    sudo systemctl restart nginx --no-block   # return immediately, do not wait

    reload is what you want for nginx config changes — it re-reads the config without dropping connections. restart drops them.

  2. Enable / disable at boot. A unit that is running now is not necessarily a unit that comes up after reboot. enable creates the symlink that makes it start automatically.

    sudo systemctl enable nginx
    sudo systemctl enable --now nginx          # enable and start in one step
    sudo systemctl disable nginx
    sudo systemctl disable --now nginx         # disable and stop

    --now is the small flag every operator wishes they had known earlier.

  3. Read status and logs together. status gives the live state and the last ~10 log lines.

    systemctl status nginx --no-pager -n 30
    journalctl -u nginx --since "10 minutes ago" --no-pager
    journalctl -u nginx -f

    journalctl -f is the tail-follow on a unit’s logs.

  4. Mask a unit you want to forcibly disable. mask symlinks the unit to /dev/null so no one can accidentally start it.

    sudo systemctl mask postfix
    # later:
    sudo systemctl unmask postfix

    Useful for “this distro insists on installing X but we never run it.”

  5. Create a drop-in to override one setting. Avoid editing the shipped unit file — drop-ins survive package upgrades.

    sudo systemctl edit nginx
    # opens an editor; type:
    [Service]
    LimitNOFILE=65535

    systemctl edit writes /etc/systemd/system/nginx.service.d/override.conf. Then:

    sudo systemctl daemon-reload
    sudo systemctl restart nginx
    systemctl cat nginx | grep -i nofile

    systemctl cat <unit> shows the resolved unit content — base file plus all drop-ins, in order.

  6. Write a minimal unit file from scratch. For an application your team owns:

    # /etc/systemd/system/app.service
    [Unit]
    Description=Stack Harbor sample application
    After=network-online.target
    Wants=network-online.target
    
    [Service]
    Type=simple
    User=app
    Group=app
    WorkingDirectory=/srv/app
    ExecStart=/usr/local/bin/app --config /etc/app/config.toml
    Restart=on-failure
    RestartSec=5
    StandardOutput=journal
    StandardError=journal
    # Modest hardening
    NoNewPrivileges=true
    PrivateTmp=true
    ProtectSystem=strict
    ReadWritePaths=/var/lib/app /var/log/app
    ProtectHome=true
    
    [Install]
    WantedBy=multi-user.target

    Then:

    sudo systemctl daemon-reload
    sudo systemctl enable --now app
    journalctl -u app --since=now -f
  7. Debugging a failed unit. The sequence:

    systemctl status app --no-pager
    journalctl -u app -p err --since "1 hour ago"
    systemctl cat app
    systemd-analyze verify /etc/systemd/system/app.service

    systemd-analyze verify parses the unit file and reports syntax/semantics issues. The exit code is non-zero if anything is wrong; the diagnostic is more specific than “failed to start.”

  8. Restart-on-failure tuning. Restart=on-failure is the typical setting, but on a misbehaving service it can crash-restart 100 times per minute. StartLimitIntervalSec= and StartLimitBurst= bound that:

    [Unit]
    StartLimitIntervalSec=60
    StartLimitBurst=3
    
    [Service]
    Restart=on-failure
    RestartSec=10

    “Restart up to 3 times in 60 seconds, then give up.” After the burst, the unit goes to failed state and systemctl reset-failed app is needed before another start.

  9. For periodic jobs, use a timer not cron. Timers are systemd units that trigger another unit on a schedule. See systemd-timers-vs-cron.

  10. For runtime resource limits, the directives are first-class. MemoryMax=, CPUQuota=, TasksMax= apply via cgroups without external tooling. A misbehaving service that consumes too much memory will be killed by the cgroup OOM rather than the host OOM.

Operational notes

  • daemon-reload is required after editing any unit file or drop-in. Without it, systemctl restart runs the old config.
  • systemctl status truncates long lines and only shows recent logs — for full output use journalctl -u <unit>.
  • systemctl restart of a unit with RemainAfterExit=yes is a no-op silently; the unit considers itself already done.
  • Services that fork themselves should declare Type=forking and a PIDFile; otherwise systemd loses track of the right PID.
  • systemd-analyze blame shows what slowed down the last boot. Useful for “why does this host take 90 seconds to come up after a reboot.”

In our fleets, every application service runs under systemd with a unit file that is generated by our provisioning code from a small role template — Type, User, Restart, hardening directives, and WorkingDirectory are all uniform. That uniformity is what makes “the service is down” a clear, actionable signal across the entire Managed Operations estate.