Skip to content

Managing Services with Systemd

Systemd is the modern standard for managing system daemons/services. It has widely replaced other startup sequences and runlevels. It is the first PID to start, and logs events to journald.

Terminology

  • systemd - daemon
  • systemctl - control action
  • target - collection of services

Getting Info

  • sudo systemctl list-units --all - display all units files
  • sudo systemctl list-unit-files --type=service - all services and current status
  • sudo systemctl list-dependencies <service> - show dependencies for a service
  • sudo systemctl mask <service> - masking "turns off" a service
    • mask / unmask
    • deal with conflicting services
    • services that use the same port etc.

Basic Usage

General

Systemd can be used to perform high-level system tasks.

  • default - Enter system default mode
  • rescue - Enter system rescue mode
  • emergency - Enter system emergency mode
  • halt - Shut down and halt the system
  • poweroff - Shut down and poweroff system
  • reboot [ARG] - Shut down and reboot system
  • suspend - Suspend the system
  • hibernate - Hibernate the system
  • hybrid-sleep - Hibernate and suspend system

Actions

sudo systemctl <ACTION> <SERVICE>

The following actions are available:

  • start - start a stopped (or previously failed) svc
  • stop - stop a running svc
  • restart - turn a svc all the off and back on
  • reload - quickly re-read new config (without turning off)
  • enable - set a service to start on boot
  • disable - prevent a service starting on boot

For example, let's restart the Elasticsearch service:

  • sudo systemctl restart elasticsearch

    ● sshd.service - OpenSSH server daemon
      Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
      Active: active (running) since Thu 2019-08-01 21:26:30 UTC; 1 weeks 5 days ago
        Docs: man:sshd(8)
              man:sshd_config(5)
    Main PID: 6105 (sshd)
      CGroup: /system.slice/sshd.service
              └─6105 /usr/sbin/sshd -D
    
    Aug 01 21:26:30 es1 systemd[1]: Starting OpenSSH server daemon...
    Aug 01 21:26:30 es1 sshd[6105]: Server listening on 0.0.0.0 port 22.
    Aug 01 21:26:30 es1 sshd[6105]: Server listening on :: port 22.
    Aug 01 21:26:30 es1 systemd[1]: Started OpenSSH server daemon.
    
    • enabled - set to start at boot
    • active - current duration
    • docs - documentation sources
    • PID - process #