LinTut

How to Manage Systemd Services with Systemctl on Linux

Systemd a standard process for managing start-up services in Linux operating systems. It is used for controlling which programs run when the Linux system boots up. It is a system manager and has become the new standard for Linux operating systems. Systemd allows you to create a custom systemd service to run and manage any process.
In this tutorial, we will explain how to manage services with systemd on Linux.

Starting with Systemtd and Systemctl Basics

By default, Systemd comes pre-installed in all major operating systems. You can verify the installed version of Systemd with the following command:

$ systemctl --version
systemd 245 (245.4-4ubuntu3.4)
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=hybrid

It’s clear from the above example, that we have systemd 245 version Installed.
Check where the binaries and libraries of systemd and systemctl are installed.

$ whereis systemd
systemd: /usr/bin/systemd /usr/lib/systemd /etc/systemd /usr/share/systemd /usr/share/man/man1/systemd.1.gz
$ whereis systemctl
systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz

You can also verify whether the Systemd is running or not with the following command:

$ ps -eaf | grep systemd
root         417       1  0 Jan30 ?        00:00:00 /lib/systemd/systemd-journald
root         464       1  0 Jan30 ?        00:00:01 /lib/systemd/systemd-udevd
systemd+     713       1  0 Jan30 ?        00:00:03 /lib/systemd/systemd-resolved
message+     825       1  0 Jan30 ?        00:00:02 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
root         852       1  0 Jan30 ?        00:00:00 /lib/systemd/systemd-logind
rasho       1043       1  0 Jan30 ?        00:00:00 /lib/systemd/systemd --user
rasho       1112    1043  0 Jan30 ?        00:00:02 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
rasho      18275   18094  0 13:32 pts/0    00:00:00 grep --color=auto systemd

[box type=”note” align=”” class=”” width=””]Note: systemd is running as parent daemon (PID=1). In the above command ps with -e select all Processes, -a select all processes except session leaders and -f for full format listing (i.e. -eaf).[/box]
You can also analyze the time taken by each process at system boot with the following command:
$ systemd-analyze blame
2.508s dev-sda2.device                                      
1.903s snapd.service                                        
1.419s fwupd-refresh.service                                
1.275s fwupd.service                                                                   
 915ms lm-sensors.service                                   
 857ms man-db.service                                       
 831ms networkd-dispatcher.service                          
 664ms logrotate.service                                    
 587ms udisks2.service                                      
 576ms systemd-logind.service
..............

To analyze the critical chain at boot with the following command:

$ systemd-analyze critical-chain
The time when unit became active or started is printed after the "@" character.
The time the unit took to start is printed after the "+" character.

graphical.target @6.768s
└─multi-user.target @6.768s
  └─snapd.seeded.service @6.722s +44ms
    └─snapd.service @4.817s +1.903s
      └─basic.target @4.752s
        └─sockets.target @4.752s
          └─snapd.socket @4.751s +1ms
            └─sysinit.target @4.745s
              └─snapd.apparmor.service @4.592s +152ms
                └─apparmor.service @4.397s +191ms
                  └─local-fs.target @4.395s
                    └─run-user-1000.mount @6.540s
                      └─local-fs-pre.target @2.187s
                        └─systemd-tmpfiles-setup-dev.service @2.053s +134ms
                          └─systemd-sysusers.service @1.863s +186ms
                            └─systemd-remount-fs.service @1.747s +112ms
                              └─systemd-journald.socket @1.730s
                                └─system.slice @1.726s
                                  └─-.slice @1.726s

List all the available units.

$ systemctl list-unit-files
UNIT FILE                                  STATE           VENDOR PRESET
proc-sys-fs-binfmt_misc.automount          static          enabled      
-.mount                                    generated       enabled      
boot-efi.mount                             generated       enabled      
dev-hugepages.mount                        static          enabled      
dev-mqueue.mount                           static          enabled      
proc-sys-fs-binfmt_misc.mount              disabled        enabled      
snap-android\x2dstudio-99.mount            enabled         enabled      

List all running units.

$ systemctl list-units
  UNIT                                                                                        LOAD   ACTIVE     SUB       DESCRIPTION                                                                >
  proc-sys-fs-binfmt_misc.automount                                                           loaded active     waiting   Arbitrary Executable File Formats File System Automount Point              >
  dev-fuse.device                                                                             loaded activating tentative /dev/fuse                                                                  >
  sys-devices-pci0000:00-0000:00:02.0-drm-card0-card0\x2dLVDS\x2d1-intel_backlight.device     loaded active     plugged   /sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-LVDS-1/intel_backlight>
  sys-devices-pci0000:00-0000:00:16.3-tty-ttyS4.device                                        loaded active     plugged   7 Series/C210 Series Chipset Family KT Controller                          >
  sys-devices-pci0000:00-0000:00:19.0-net-enp0s25.device                                      loaded active     plugged   82579LM Gigabit Network Connection (Lewisville)                            >
  sys-devices-pci0000:00-0000:00:1b.0-sound-card0.device                                      loaded active     plugged   7 Series/C216 Chipset Family High Definition Audio Controller              >
  sys-devices-pci0000:00-0000:00:1c.3-0000:03:00.0-net-wlo1.device                            loaded active     plugged   Centrino Advanced-N 6235 (AGN)                                             >
  sys-devices-pci0000:00-0000:00:1d.0-usb2-2\x2d1-2\x2d1.6-2\x2d1.6:1.0-bluetooth-hci0.device loaded active     plugged   /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.6/2-1.6:1.0/bluetooth/hci>
  sys-devices-pci0000:00-0000:00:1f.2-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda1.device    loaded active     plugged   PNY_CS900_120GB_SSD EFI\x20System\x20Partition                             >
  sys-devices-pci0000:00-0000:00:1f.2-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda2.device    loaded active     plugged   PNY_CS900_120GB_SSD 2                                                      >
  sys-devices-pci0000:00-0000:00:1f.2-ata1-host0-target0:0:0-0:0:0:0-block-sda.device         loaded active     plugged   PNY_CS900_120GB_SSD

List all failed units.

$ systemctl --failed
  UNIT                  LOAD   ACTIVE SUB    DESCRIPTION                           
● fwupd-refresh.service loaded failed failed Refresh fwupd metadata and update motd

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.

1 loaded units listed.

[box type=”info” align=”” class=”” width=””]The output has the following columns :
UNIT: The systemd service unit name
LOAD: Shows whether the unit definition was properly read and loaded
ACTIVE: Describes if the unit is active.
SUB: Low-level activation state of the unit, giving more detailed information about the unit. This will vary by unit type.
DESCRIPTION: The service unit’s description.[/box]

Manage Services with Systemd

Systemctl provides an easier way to manage the systemd services.
List all services (including enabled and disabled).

$ systemctl list-unit-files --type=service
UNIT FILE                                  STATE           VENDOR PRESET
accounts-daemon.service                    enabled         enabled      
acpid.service                              disabled        enabled      
alsa-restore.service                       static          enabled      
alsa-state.service                         static          enabled      
alsa-utils.service                         masked          enabled      
anacron.service                            enabled         enabled      
apparmor.service                           enabled         enabled      
apt-daily-upgrade.service                  static          enabled      
apt-daily.service                          static          enabled      
autovt@.service                            enabled         enabled      
avahi-daemon.service                       enabled         enabled      
blk-availability.service                   enabled         enabled      
bluetooth.service                          enabled         enabled      
bolt.service                               static          enabled
...................

To start a systemd service, use the systemctl start command:

$ sudo systemctl start name.service

You can leave .service suffix. For example, to start the apache server on Ubuntu:

$ sudo systemctl start apache2

To stop a running service:

$ sudo systemctl stop apache2

To restart the Apache service, run the following command:

$ systemctl restart apache2

To reload the Apache service, run the following command:

$ systemctl reload apache2

To enable the Apache service to start after system reboot, run the following command:

$ systemctl enable apache2

To remove the Apache service from the system start up, run the following command:

$ systemctl disable apache2

To kill the Apache service, run the following command:

$ systemctl kill apache2

To mask the Apache service, run the following command:

$ systemctl mask apache2

To unmask the Apache process, run the following command:

$ systemctl unmask apache2

To check whether the Apache service is enabled or not, run the following command:

$ systemctl is-enabled apache2

To check the status of the Apache service, run the following command:

$ systemctl status apache2

After renaming or deleting any systemd file, you will need to reload it to apply the changes. To reload the systemd service file, run the following command:

$ systemctl daemon-reload

Manage Sockets with Systemctl

You can also manage sockets using the Systemctl command.
To list all available system sockets, run the following command:

$ systemctl list-unit-files --type=socket
UNIT FILE                        STATE    VENDOR PRESET
acpid.socket                     enabled  enabled      
avahi-daemon.socket              enabled  enabled
ssh.socket                       disabled enabled            
cups.socket                      enabled  enabled      
dbus.socket                      static   enabled      
dm-event.socket                  enabled  enabled      
lvm2-lvmpolld.socket             enabled  enabled      
saned.socket                     disabled enabled      
snapd.socket                     enabled  enabled      
syslog.socket                    static   disabled     
systemd-coredump.socket          static   enabled      
systemd-fsckd.socket             static   enabled      
systemd-initctl.socket           static   enabled      

To start the SSH socket, run the following command:

$ systemctl start ssh.socket

To stop the SSH socket, run the following command:

$ systemctl stop ssh.socket

To restart the SSH socket, run the following command:

$ systemctl restart ssh.socket

To check the status of the SSH socket, run the following command:

$ systemctl status ssh.socket

To enable and disable the SSH socket at boot time, run the following command:

$ systemctl enable ssh.socket
$ systemctl disable ssh.socket

Control System Runlevels

How to start a system rescue mode.

# systemctl rescue

Broadcast message from root@tecmint on pts/0 (Wed 2015-04-29 11:31:18 IST):

The system is going down to rescue mode NOW!
How to enter into emergency mode.

$ systemctl emergency
Welcome to emergency mode! After logging in, type "journalctl -xb" to view
system logs, "systemctl reboot" to reboot, "systemctl default" to try again
to boot into default mode.

List current run levels in use.

$ systemctl get-default

multi-user.target

How to start Runlevel 5 aka graphical mode.

$ systemctl isolate runlevel5.target
OR
$ systemctl isolate graphical.target

How to start Runlevel 3 aka multiuser mode (command line).

$ systemctl isolate runlevel3.target
OR
$ systemctl isolate multiuser.target

How to set multiuser mode or graphical mode as default run level.

$ systemctl set-default runlevel3.target
OR
$ systemctl set-default runlevel5.target

How to reboot, halt, suspend, hibernate, or put a system in hybrid-sleep.

$ systemctl reboot
$ systemctl halt
$ systemctl suspend
$ systemctl hibernate
$ systemctl hybrid-sleep

For those who may not be aware of run levels and what it does.

[box type=”info” align=”” class=”” width=””]
For those who may not be aware of run levels and what it does.

Runlevel 0 : Shut down and Power off the system.
Runlevel 1 : Rescue?Maintainance Mode.
Runlevel 3 : multiuser, no-graphic system.
Runlevel 4 : multiuser, no-graphic system.
Runlevel 5 : multiuser, graphical system.
Runlevel 6 : Shutdown and Reboot the machine.[/box]

Conclusion

In the above guide, you learned how to manage and control systemd service on Linux. I hope this will helps you to interact with and control your systemd instance. Feel free to ask me if you have any questions.

See also: How to Kill a Process in Linux

Exit mobile version