Chapter 13. Managing system services with systemctl

The systemctl utility helps managing system services. You can use the systemctl utility to perform different tasks related to different services such as starting, stopping, restarting, enabling, and disabling services, listing services, and displaying system services statuses.

This section describes how to manage system services with the systemctl utility.

13.1. Service unit management with systemctl

The service units help to control the state of services and daemons in your system.

Service units end with the .service file extension, for example nfs-server.service. However, while using service file names in commands, you can omit the file extension. The systemctl utility assumes the argument is a service unit. For example, to stop the nfs-server.service, enter the following command:

# systemctl stop nfs-server

Additionally, some service units have alias names. Aliases can be shorter than units, and you can use them instead of the actual unit names.

To find all aliases that can be used for a particular unit, use:

# systemctl show nfs-server.service -p Names

13.2. Comparison of a service utility with systemctl

This section shows a comparison between a service utility and the usage of systemctl command.

Table 13.1. Comparison of the service utility with systemctl

servicesystemctlDescription

service <name> start

systemctl start <name>.service

Starts a service.

service <name> stop

systemctl stop <name>.service

Stops a service.

service <name> restart

systemctl restart <name>.service

Restarts a service.

service <name> condrestart

systemctl try-restart <name>.service

Restarts a service only if it is running.

service <name> reload

systemctl reload <name>.service

Reloads configuration.

service <name> status

systemctl status <name>.service

systemctl is-active <name>.service

Checks if a service is running.

service --status-all

systemctl list-units --type service --all

Displays the status of all services.

13.3. Listing system services

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

Procedure

  • To list all currently loaded service units, enter:

    $ 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 displays:

    • UNIT: its full name
    • LOAD: information whether the unit file has been loaded
    • ACTIVE\ SUB: its high-level and low-level unit file activation state
    • DESCRIPTION: a short description
  • To list all loaded units regardless of their state, enter the following command with the --all or -a command line option:

    $ systemctl list-units --type service --all
  • To list the status (enabled / disabled) of all available service units, enter:

    $ 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: its full name
    • STATE: information whether the service unit is enabled or disabled

Additional resources

13.4. Displaying system service status

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

Procedure

  • To display detailed information about a service unit that corresponds to a system service, enter:

    $ systemctl status <name>.service

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

    This command displays the name of the selected service unit followed by its short description, one or more fields described in Available service unit information, if it is executed by the root user, and the most recent log entries.

    Table 13.2. 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.

    Active

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

    Main PID

    The PID of the corresponding system service followed by its name.

    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
               ├─1037 /usr/libexec/gdm-simple-slave --display-id /org/gno...
               └─1047 /usr/bin/Xorg :0 -background none -verbose -auth /r...
    
    Oct 17 17:31:23 localhost systemd[1]: Started GNOME Display Manager.
  • To only verify that a particular service unit is running, enter:

    $ systemctl is-active <name>.service
  • To determine whether a particular service unit is enabled, enter:

    $ systemctl is-enabled <name>.service
    Note

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

  • To determine what services are ordered to start before the specified service unit, enter:

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

    Replace <name> with the name of the service in the command. 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]
  • To determine what services are ordered to start after the specified service unit, enter:

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

    Replace <name> with the name of the service in the command. For example, to view the list of services ordered 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.5. Positive and negative service dependencies

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.6. Starting a system service

You can start system service in the current session using the start command. You must have a root access as starting a service may affect the state of the operating system.

Procedure

  • To start a selected service unit corresponding to a system service, type the following command as root:

    # systemctl start <name>.service

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

    Example 13.2. Starting httpd.service

    The service unit for the Apache HTTP Server is named httpd.service. To activate this service unit and start the httpd daemon in the current session, enter the following command as root:

    # systemctl start httpd.service

13.7. Stopping a system service

You can stop system service in the current session using the stop command. You must have a root access as stopping a service may affect the state of the operating system.

Procedure

  • To stop the service unit corresponding to a system service, enter the following command as root:

    # systemctl stop <name>.service

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

    Example 13.3. Stopping bluetoothd.service

    The service unit for the bluetoothd daemon is named bluetooth.service. To deactivate this service unit and stop the bluetoothd daemon in the current session, enter the following command as root:

    # systemctl stop bluetooth.service

13.8. Restarting a system service

You can restart system service in the current session using the restart command. You must have a root access as restarting a service may affect the state of the operating system.

This procedure describes how to:

  • 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

Procedure

  • To restart a service unit corresponding to a system service, enter the following command as root:

    # 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.

    • Alternatively, to restart a service unit only if the corresponding service is already running, enter the following command as root:

      # systemctl try-restart <name>.service
    • To reload the configuration without interrupting service execution, enter the following command as root:

      # 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.

    Example 13.4. Reloading httpd.service

    In order to prevent users from encountering unnecessary error messages or partially rendered web pages, the Apache HTTP Server allows you to edit and reload its configuration without the need to restart it and interrupt actively processed requests. To do so, enter the following as root:

    # systemctl reload httpd.service

Additional resources

13.9. Enabling a system service

You can configure service to start automatically at the system booting time. The enable command reads the [Install] section of the selected service unit and creates appropriate symbolic links to the /usr/lib/systemd/system/name.service file in the /etc/systemd/system/ directory and its sub-directories. However, it does not rewrite links that already exist.

Procedure

  • To configure a service unit that corresponds to a system service to be automatically started at boot time, enter the following command as root:

    # systemctl enable <name>.service

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

    • If you want to ensure that the symbolic links are re-created, enter the following command as root:

      # systemctl reenable <name>.service

      This command disables the selected service unit and immediately enables it again.

      Example 13.5. Enabling httpd.service

      To configure the Apache HTTP Server to start automatically at boot time, enter the following command as root:

      # systemctl enable httpd.service
      Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

13.10. Disabling a system service

You can prevent a service unit from starting automatically at boot time. The disable command reads the [Install] section of the selected service unit and removes appropriate symbolic links to the /usr/lib/systemd/system/name.service file from the /etc/systemd/system/ directory and its sub-directories.

Procedure

  • To configure a service unit that corresponds to a system service not to start automatically at boot time, enter the following command as root:

    # systemctl disable <name>.service

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

    Example 13.6. Disabling bluetoothd.service

    The service unit for the bluetoothd daemon is named bluetooth.service. To prevent this service unit from starting at boot time, enter the following command as a root:

    # systemctl disable bluetooth.service
    Removed symlink /etc/systemd/system/bluetooth.target.wants/bluetooth.service.
    Removed symlink /etc/systemd/system/dbus-org.bluez.service.
    • To mask any service unit and prevent it from being started manually or by another service, enter the following command as root:

      # 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.

    • To revert this action and unmask a service unit, enter:

      # systemctl unmask <name>.service