Managing Services

systemd

systemd is a replacement to System V. systemd stands for system daemon. Its name is intentionally in lowercase. systemd was designed to allow for better handling of dependencies and have the ability to handle more work in parallel at startup. systemd supports snap shotting of your system and the restoring of your systems state, keeps track of processes stored in what is known as a "cgroup" as opposed to the conventional "PID" method.

The command used by systemd for controlling services is "systemctl" Below are a few examples of some of the basic functionality of the "systemctl" command.

systemd command Description
systemctl start mytest.service Starts specified service
systemctl stop mytest.service Stops specified service
systemctl status mytest.service Request status of specified service
systemctl list-unit-files --type=service Lists known services that can be started or stopped
systemctl restart mytest.service Restarts the specified service
systemctl reload mytest.service If supported will re-load the configuration file
systemctl enable mytest.service Enables specified service
systemctl disable mytest.service Disables specified service
systemctl is-enabled mytest.service Checks to see if service is configured to start in current target level (runlevel)

systemctl example commands

Below are some examples of the more commonly used "systemctl" commands for managing services.

Enabling and Disabling

To "enable" or "disable" a service, the following "systemctl" commands can be used. In the example, we will use a service called "chronyd".

Verify if service is enabled


# systemctl is-enabled chronyd
enabled

Disabling a Service

Disabling a service will stop the service from automatically starting at system start-up.


# systemctl disable chronyd
Removed /etc/systemd/system/multi-user.target.wants/chronyd.service.

# systemctl is-enabled chronyd
disabled

Enabling a Service

Enabling a service will automatically start the service at system start-up.


# systemctl enable chronyd
Created symlink /etc/systemd/system/multi-user.target.wants/chronyd.service → /usr/lib/systemd/system/chronyd.service.

# systemctl is-enabled chronyd
enabled

Starting and Stopping

To start and stop a service, the "systemctl" command is used. The parameters start/stop/restart can be passed followed by the name of the service. To verify the service has started successfully, you can use the"status" parameter.

Starting a Service


# systemctl start chronyd

# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2021-05-08 19:40:38 BST; 3s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 1347 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 1343 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 1345 (chronyd)
    Tasks: 1 (limit: 9206)
   Memory: 1.0M
   CGroup: /system.slice/chronyd.service
           └─1345 /usr/sbin/chronyd

May 08 19:40:38 centos8a systemd[1]: Starting NTP client/server...
May 08 19:40:38 centos8a chronyd[1345]: chronyd version 3.5 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDRO>
May 08 19:40:38 centos8a chronyd[1345]: Frequency 10.700 +/- 10.972 ppm read from /var/lib/chrony/drift
May 08 19:40:38 centos8a chronyd[1345]: Using right/UTC timezone to obtain leap second data
May 08 19:40:38 centos8a systemd[1]: Started NTP client/server.

Stopping a Service


# systemctl stop chronyd

# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Sat 2021-05-08 19:42:46 BST; 3s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 1347 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 1343 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 1345 (code=exited, status=0/SUCCESS)

May 08 19:40:38 centos8a chronyd[1345]: chronyd version 3.5 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDRO>
May 08 19:40:38 centos8a chronyd[1345]: Frequency 10.700 +/- 10.972 ppm read from /var/lib/chrony/drift
May 08 19:40:38 centos8a chronyd[1345]: Using right/UTC timezone to obtain leap second data
May 08 19:40:38 centos8a systemd[1]: Started NTP client/server.
May 08 19:40:43 centos8a chronyd[1345]: Selected source 193.47.147.20
May 08 19:40:43 centos8a chronyd[1345]: System clock TAI offset set to 37 seconds
May 08 19:41:49 centos8a chronyd[1345]: Selected source 81.21.65.169
May 08 19:42:46 centos8a systemd[1]: Stopping NTP client/server...
May 08 19:42:46 centos8a systemd[1]: chronyd.service: Succeeded.
May 08 19:42:46 centos8a systemd[1]: Stopped NTP client/server.

Restarting a Service

The "restart" parameter when run will stop a running service and re-start the service. If the service is already stopped, the "restart" parameter, will start the service.

In the example below, you can see that the service was already running, so it was stopped and then immediately started.


# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2021-05-08 20:11:25 BST; 12s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 1448 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 1443 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 1446 (chronyd)
    Tasks: 1 (limit: 9206)
   Memory: 964.0K
   CGroup: /system.slice/chronyd.service
           └─1446 /usr/sbin/chronyd

May 08 20:11:25 centos8a systemd[1]: chronyd.service: Succeeded.
May 08 20:11:25 centos8a systemd[1]: Stopped NTP client/server.
May 08 20:11:25 centos8a systemd[1]: Starting NTP client/server...
May 08 20:11:25 centos8a chronyd[1446]: chronyd version 3.5 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDRO>
May 08 20:11:25 centos8a chronyd[1446]: Frequency 11.716 +/- 1.743 ppm read from /var/lib/chrony/drift
May 08 20:11:25 centos8a chronyd[1446]: Using right/UTC timezone to obtain leap second data
May 08 20:11:25 centos8a systemd[1]: Started NTP client/server.
May 08 20:11:30 centos8a chronyd[1446]: Selected source 193.47.147.20
May 08 20:11:30 centos8a chronyd[1446]: System clock TAI offset set to 37 seconds

Display all Service units

To display all service units on your system, issue the following command: systemctl --type=service


# systemctl --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                       
  dracut-shutdown.service            loaded active exited  Restore /run/initramfs on shutdown             
  getty@tty1.service                 loaded active running Getty on tty1                                  
  import-state.service               loaded active exited  Import network configuration from initramfs    
  irqbalance.service                 loaded active running irqbalance daemon                              

.....
.....

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.
42 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

The above is only a sample of the output displayed from the above command.

Display failed service units

To produce a list of failed service units, issue the following command: systemctl --failed --type=service


# systemctl --failed --type=service
  UNIT           LOAD   ACTIVE SUB    DESCRIPTION                           
● kdump.service  loaded failed failed Crash recovery kernel arming          
● mcelog.service loaded failed failed Machine Check Exception Logging Daemon

Show available options for a service

To display all available options available to a given service, issue the following command: systemctl show service-name.

List dependencies for a service

To list all dependencies for a service, issue the following command: systemctl list-dependencies service-name.

Display service unit file

To display the associated start-up unit file for a service, issue the following command: systemctl cat --no-pager service-name.


[root@centos8a ~]# systemctl cat --no-pager chronyd
# /usr/lib/systemd/system/chronyd.service
[Unit]
Description=NTP client/server
Documentation=man:chronyd(8) man:chrony.conf(5)
After=ntpdate.service sntp.service ntpd.service
Conflicts=ntpd.service systemd-timesyncd.service
ConditionCapability=CAP_SYS_TIME

[Service]
Type=forking
PIDFile=/run/chrony/chronyd.pid
EnvironmentFile=-/etc/sysconfig/chronyd
ExecStart=/usr/sbin/chronyd $OPTIONS
ExecStartPost=/usr/libexec/chrony-helper update-daemon
PrivateTmp=yes
ProtectHome=yes
ProtectSystem=full

[Install]
WantedBy=multi-user.target