Bookmark this page

Chapter 9.  Control Services and Daemons

Abstract

Goal

Control and monitor network services and system daemons with the systemd service.

Objectives
  • List system daemons and network services that the systemd service and socket units started.

  • Control system daemons and network services with the systemctl command.

Sections
  • Identify Automatically Started System Processes (and Guided Exercise)

  • Control System Services (and Guided Exercise)

Lab
  • Control Services and Daemons

Identify Automatically Started System Processes

Objectives

  • List system daemons and network services that were started by the systemd service and socket units.

Introduction to the systemd Daemon

The systemd daemon manages the startup process for Linux, including service startup and service management in general. The systemd daemon activates system resources, server daemons, and other processes, both at boot time and on a running system.

Daemons are processes that either wait or run in the background, to perform various tasks. Generally, daemons start automatically at boot time and continue to run until shutdown or until you manually stop them. By convention, daemon names end with d.

A service in the systemd sense often refers to one or more daemons. However, starting or stopping a service might instead change the state of the system once, without leaving a running daemon process afterward (called oneshot).

In Red Hat Enterprise Linux, the first process that starts (PID 1) is the systemd daemon, which provides these features:

  • Parallelization capabilities (starting multiple services simultaneously), which increase the boot speed of a system.

  • On-demand starting of daemons without requiring a separate service.

  • Automatic service dependency management, which can prevent long timeouts. For example, a network-dependent service does not try to start until the network is available.

  • A method of tracking related processes together by using Linux control groups.

Service Units Description

A systemd unit is an abstract concept to define objects that the system knows how to manage.

Units are represented by configuration files called unit files, which encapsulate information about system services, listening sockets, and other relevant objects for the systemd init system.

A unit has a name and a unit type. The name provides a unique identity to the unit. The unit type enables grouping units together with other similar unit types.

The systemd daemon uses units to manage different types of objects:

  • Service units have a .service extension and represent system services. You can use service units to start often-accessed daemons, such as a web server.

  • Socket units have a .socket extension and represent inter-process communication (IPC) sockets that systemd should monitor. If a client connects to the socket, then the systemd manager starts a daemon and passes the connection to it. You can use socket units to delay the start of a service at boot time and to start less often used services on demand.

  • Path units have a .path extension and delay the activation of a service until a specific file-system change occurs. You can use path units for services that use spool directories, such as a printing system.

To manage units, use the systemctl command. For example, display available unit types with the systemctl -t help command. The systemctl command can take abbreviated unit names, process tree entries, and unit descriptions.

List Service Units

Use the systemctl command to explore the system's current state. For example, the following command lists and paginates all currently loaded service units.

[root@host ~]# systemctl list-units --type=service
UNIT                  LOAD    ACTIVE  SUB      DESCRIPTION
atd.service           loaded  active  running  Job spooling tools
auditd.service        loaded  active  running  Security Auditing Service
chronyd.service       loaded  active  running  NTP client/server
crond.service         loaded  active  running  Command Scheduler
dbus.service          loaded  active  running  D-Bus System Message Bus
...output omitted...

In this example, the --type=service option limits the type of systemd units to service units. The output has the following columns:

UNIT

The service unit name.

LOAD

Whether the systemd daemon correctly parsed the unit's configuration and loaded the unit into memory.

ACTIVE

The high-level activation state of the unit. This information indicates whether the unit started successfully.

SUB

The low-level activation state of the unit. This information indicates more detailed information about the unit. The information varies based on unit type, state, and how the unit is executed.

DESCRIPTION

The short description of the unit.

By default, the systemctl list-units --type=service command lists only the service units with active activation states. The systemctl list-units --all option lists all service units regardless of the activation states. Use the --state= option to filter by the values in the LOAD, ACTIVE, or SUB fields.

[root@host ~]# systemctl list-units --type=service --all
UNIT                          LOAD      ACTIVE   SUB     DESCRIPTION
  atd.service                 loaded    active   running Job spooling tools
  auditd.service              loaded    active   running Security Auditing ...
  auth-rpcgss-module.service  loaded    inactive dead    Kernel Module ...
  chronyd.service             loaded    active   running NTP client/server
  cpupower.service            loaded    inactive dead    Configure CPU power ...
  crond.service               loaded    active   running Command Scheduler
  dbus.service                loaded    active   running D-Bus System Message Bus
● display-manager.service     not-found inactive dead    display-manager.service
...output omitted...

The systemctl command without any arguments lists units that are both loaded and active.

[root@host ~]# systemctl
UNIT                                 LOAD   ACTIVE SUB       DESCRIPTION
proc-sys-fs-binfmt_misc.automount    loaded active waiting   Arbitrary...
sys-devices-....device               loaded active plugged   Virtio network...
sys-subsystem-net-devices-ens3.deviceloaded active plugged   Virtio network...
...output omitted...
-.mount                              loaded active mounted   Root Mount
boot.mount                           loaded active mounted   /boot
...output omitted...
systemd-ask-password-plymouth.path   loaded active waiting   Forward Password...
systemd-ask-password-wall.path       loaded active waiting   Forward Password...
init.scope                           loaded active running   System and Servi...
session-1.scope                      loaded active running   Session 1 of...
atd.service                          loaded active running   Job spooling tools
auditd.service                       loaded active running   Security Auditing...
chronyd.service                      loaded active running   NTP client/server
crond.service                        loaded active running   Command Scheduler
...output omitted...

The systemctl command list-units option displays units that the systemd service attempts to parse and load into memory. This option does not display services that are installed but not enabled. You can use the systemctl command list-unit-files option to see the state of all the installed unit files:

[root@host ~]# systemctl list-unit-files --type=service
UNIT FILE                         STATE       VENDOR PRESET
arp-ethers.service                disabled    disabled
atd.service                       enabled     enabled
auditd.service                    enabled     enabled
auth-rpcgss-module.service        static      -
autovt@.service                   alias       -
blk-availability.service          disabled    disabled
...output omitted...

In the output of the systemctl list-unit-files command, some common entries for the STATE field are enabled, disabled, static, and masked. All STATE values are listed in the systemctl command manual pages.

View Service States

View a unit's status with the systemctl status name.type command. If the unit type is omitted, then the command expects a service unit with that name.

[root@host ~]# systemctl status sshd.service
● sshd.service - OpenSSH server daemon
     Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-03-14 05:38:12 EDT; 25min ago
       Docs: man:sshd(8)
             man:sshd_config(5)
   Main PID: 1114 (sshd)
      Tasks: 1 (limit: 35578)
     Memory: 5.2M
        CPU: 64ms
     CGroup: /system.slice/sshd.service
             └─1114 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"

Mar 14 05:38:12 workstation systemd[1]: Starting OpenSSH server daemon...
Mar 14 05:38:12 workstation sshd[1114]: Server listening on 0.0.0.0 port 22.
Mar 14 05:38:12 workstation sshd[1114]: Server listening on :: port 22.
Mar 14 05:38:12 workstation systemd[1]: Started OpenSSH server daemon.
...output omitted...

Some fields from the systemctl command status option output:

Table 9.1. Service Unit Information

FieldDescription
LoadedWhether the service unit is loaded into memory
ActiveWhether the service unit is running, and if so, for how long
DocsWhere to find more information about the service
Main PIDThe main process ID of the service, including the command name
StatusMore information about the service
ProcessMore information about related processes
CGroupMore information about related control groups

Not all these fields are always present in the command output.

Keywords in the status output indicate the state of the service:

Table 9.2. Service States in the Output of systemctl

KeywordDescription
loadedThe unit configuration file is processed.
active (running)The service is running with continuing processes.
active (exited)The service successfully completed a one-time configuration.
active (waiting)The service is running but is waiting for an event.
inactiveThe service is not running.
enabledThe service starts at boot time.
disabledThe service is not set to start at boot time.
staticThe service cannot be enabled, but an enabled unit might start it automatically.

Note

The systemctl status NAME command replaces the service NAME status command from Red Hat Enterprise Linux 6 and earlier versions.

Verify the Status of a Service

The systemctl command verifies the specific states of a service. For example, use the systemctl command is-active option to verify whether a service unit is active (running):

[root@host ~]# systemctl is-active sshd.service
active

The command returns the service unit state, which is usually active or inactive.

Run the systemctl command is-enabled option to verify whether a service unit is enabled to start automatically during system boot:

[root@host ~]# systemctl is-enabled sshd.service
enabled

The command returns whether the service unit is enabled to start at boot time, and the state is usually enabled or disabled.

To verify whether the unit failed during startup, run the systemctl command is-failed option:

[root@host ~]# systemctl is-failed sshd.service
active

The command returns active if the service is correctly running, or failed if an error occurred during startup. If the unit was stopped, then it returns unknown or inactive.

To list all the failed units, run the systemctl --failed --type=service command.

References

systemd(1), systemd.unit(5), systemd.service(5), systemd.socket(5), and systemctl(1) man pages

For more information, refer to the Managing Services with systemd chapter in the Red Hat Enterprise Linux 9 Configuring Basic System Settings Guide at https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html-single/configuring_basic_system_settings/managing-services-with-systemd_configuring-basic-system-settings#managing-services-with-systemd_configuring-basic-system-settings

Revision: rh124-9.3-770cc61