Chapter 13. Managing system services with systemctl

As a system administrator, you can manage system services by using the systemctl utility. You can perform various tasks, such as starting, stopping, restarting running services, enabling and disabling services to start at boot, listing available services, and displaying system services statuses.

13.1. Listing system services

You can list all currently loaded service units and display the status of all available service units.

Procedure

Use the systemctl command to perform any of the following tasks:

  • List all currently loaded service units:

    $ systemctl list-units --type service
    UNIT                     LOAD   ACTIVE SUB     DESCRIPTION
    abrt-ccpp.service        loaded active exited  Install ABRT coredump hook
    abrt-oops.service        loaded active running ABRT kernel log watcher
    abrtd.service            loaded active running ABRT Automated Bug Reporting Tool
    ...
    systemd-vconsole-setup.service loaded active exited  Setup Virtual Console
    tog-pegasus.service            loaded active running OpenPegasus CIM Server
    
    LOAD   = Reflects whether the unit definition was properly loaded.
    ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
    SUB    = The low-level unit activation state, values depend on unit type.
    
    46 loaded units listed. Pass --all to see loaded but inactive units, too.
    To show all installed unit files use 'systemctl list-unit-files'

    By default, the systemctl list-units command displays only active units. For each service unit file, the command provides an overview of the following parameters:

    UNIT
    The full name of the service unit
    LOAD
    The load state of the configuration file
    ACTIVE or SUB
    The current high-level and low-level unit file activation state
    DESCRIPTION
    A short description of the unit’s purpose and functionality
  • List all loaded units regardless of their state, by using the following command with the --all or -a command line option:

    $ systemctl list-units --type service --all
  • List the status (enabled or disabled) of all available service units:

    $ systemctl list-unit-files --type service
    UNIT FILE                               STATE
    abrt-ccpp.service                       enabled
    abrt-oops.service                       enabled
    abrtd.service                           enabled
    ...
    wpa_supplicant.service                  disabled
    ypbind.service                          disabled
    
    208 unit files listed.

    For each service unit, this command displays:

    UNIT FILE
    The full name of the service unit
    STATE
    The information whether the service unit is enabled or disabled to start automatically during boot

Additional resources

13.2. Displaying system service status

You can inspect any service unit to get detailed information and verify the state of the service, whether it is enabled to start during boot or currently running. You can also view services that are ordered to start after or before a particular service unit.

Procedure

Use the systemctl command to perform any of the following tasks:

  • Display detailed information about a service unit that corresponds to a system service:

    $ systemctl status <name>.service

    Replace <name> with the name of the service unit you want to inspect (for example, gdm).

    This command displays the following information:

    • The name of the selected service unit followed by a short description
    • One or more fields described in Available service unit information
    • The execution of the service unit: if the unit is executed by the root user
    • The most recent log entries

      Table 13.1. Available service unit information

      FieldDescription

      Loaded

      Information whether the service unit has been loaded, the absolute path to the unit file, and a note whether the unit is enabled to start during boot.

      Active

      Information whether the service unit is running followed by a time stamp.

      Main PID

      The process ID and the name of the corresponding system service.

      Status

      Additional information about the corresponding system service.

      Process

      Additional information about related processes.

      CGroup

      Additional information about related control groups (cgroups).

    Example 13.1. Displaying service status

    The service unit for the GNOME Display Manager is named gdm.service. To determine the current status of this service unit, type the following at a shell prompt:

    # systemctl status gdm.service
    gdm.service - GNOME Display Manager
       Loaded: loaded (/usr/lib/systemd/system/gdm.service; enabled)
       Active: active (running) since Thu 2013-10-17 17:31:23 CEST; 5min ago
     Main PID: 1029 (gdm)
       CGroup: /system.slice/gdm.service
               ├─1029 /usr/sbin/gdm
               └─1047 /usr/bin/Xorg :0 -background none -verbose -auth /r...
    
    Oct 17 17:31:23 localhost systemd[1]: Started GNOME Display Manager.
  • Verify that a particular service unit is running:

    $ systemctl is-active <name>.service
  • Determine whether a particular service unit is enabled to start during boot:

    $ systemctl is-enabled <name>.service
    Note

    Both systemctl is-active and systemctl is-enabled commands return an exit status of 0 if the specified service unit is running or enabled.

  • Check what services systemd orders to start before the specified service unit

    # systemctl list-dependencies --after <name>.service

    For example, to view the list of services ordered to start before gdm, enter:

    # systemctl list-dependencies --after gdm.service
    gdm.service
    ├─dbus.socket
    ├─getty@tty1.service
    ├─livesys.service
    ├─plymouth-quit.service
    ├─system.slice
    ├─systemd-journald.socket
    ├─systemd-user-sessions.service
    └─basic.target
    [output truncated]
  • Check what services systemd orders to start after the specified service unit:

    # systemctl list-dependencies --before <name>.service

    For example, to view the list of services systemd orders to start after gdm, enter:

    # systemctl list-dependencies --before gdm.service
    gdm.service
    ├─dracut-shutdown.service
    ├─graphical.target
    │ ├─systemd-readahead-done.service
    │ ├─systemd-readahead-done.timer
    │ └─systemd-update-utmp-runlevel.service
    └─shutdown.target
      ├─systemd-reboot.service
      └─final.target
        └─systemd-reboot.service

Additional resources

13.3. Starting a system service

You can start system service in the current session by using the start command.

Prerequisites

  • Root access

Procedure

  • Start a system service in the current session:

    # systemctl start <name>.service

    Replace <name> with the name of the service unit you want to start (for example, httpd.service).

    Note

    In systemd, positive and negative dependencies between services exist. Starting a particular service may require starting one or more other services (positive dependency) or stopping one or more services (negative dependency).

    When you attempt to start a new service, systemd resolves all dependencies automatically, without explicit notification to the user. This means that if you are already running a service, and you attempt to start another service with a negative dependency, the first service is automatically stopped.

    For example, if you are running the postfix service, and you attempt to start the sendmail service, systemd first automatically stops postfix, because these two services are conflicting and cannot run on the same port.

Additional resources

13.4. Stopping a system service

If you want to stop a system service in the current session, use the stop command.

Prerequisites

  • Root access

Procedure

  • Stop a system service:

    # systemctl stop <name>.service

    Replace <name> with the name of the service unit you want to stop (for example, bluetooth).

Additional resources

13.5. Restarting a system service

You can restart system service in the current session using the restart command to perform the following actions:

  • Stop the selected service unit in the current session and immediately start it again.
  • Restart a service unit only if the corresponding service is already running.
  • Reload configuration of a system service without interrupting its execution.

Prerequisites

  • Root access

Procedure

  • Restart a system service:

    # systemctl restart <name>.service

    Replace <name> with the name of the service unit you want to restart (for example, httpd).

    Note

    If the selected service unit is not running, this command starts it too.

  • Optional: Restart a service unit only if the corresponding service is already running:

    # systemctl try-restart <name>.service
  • Optional: Reload the configuration without interrupting service execution:

    # systemctl reload <name>.service
    Note

    System services that do not support this feature, ignore this command. To restart such services, use the reload-or-restart and reload-or-try-restart commands instead.

Additional resources

13.6. Enabling a system service to start at boot

You can enable a service to start automatically at boot, these changes apply with the next reboot.

Prerequisites

  • Root access
  • The service you want to enable must not be masked. If you have a masked service, unmask it first:

    # systemctl unmask <name>.service

Procedure

  • Enable a service to start at boot:

    # systemctl enable <name>.service

    Replace <name> with the name of the service unit you want to enable (for example, httpd).

  • Optional: You can also enable and start a service by using a single command:

    # systemctl enable --now <name>.service

Additional resources

13.7. Disabling a system service to start at boot

You can prevent a service unit from starting automatically at boot time. If you disable a service, it will not start at boot, but you can start it manually. You can also mask a service, so that it cannot be started manually. Masking is a way of disabling a service that makes the service permanently unusable until it is unmasked again.

Prerequisites

  • Root access

Procedure

  • Disable a service to start at boot:

    # systemctl disable <name>.service

    Replace <name> with the name of the service unit you want to disable (for example, bluetooth).

  • Optional: If you want to make a service permanently unusable, mask the service:

    # systemctl mask <name>.service

    This command replaces the /etc/systemd/system/name.service file with a symbolic link to /dev/null, rendering the actual unit file inaccessible to systemd.

Additional resources