How To Use Systemctl to Manage Systemd Services and Units. In this post, we will introduce systemd, its advantages then show you how to manage systemd services and units using systemctl.
Well let’s start with How To Use Systemctl to Manage Systemd Services and Units.
Systemed in Linux is a system and service manager designed particularly for the Linux kernel. Proposed in 2010, Systemd replaced the widely used sysvinit system to become the first process. Aadditionally, it was executed in user space during the Linux start up process.
Kernel started the sysvinit as the first process to boot up any Linux or Unix computer. After successfully booting the system, it continually runs and waits for appropriate commands like ‘shutdown.’ all in all, it shuts down the Linux system. Simply put, Sysvinit’s process effortlessly shuts down all the tasks in the system.
Basically, it remained the most suitable choice to bring up and shut down the Linux based system for many years. However, with time, it became slow and inflexible for modern computers.
Although both sysvinit and systemd have their advantages, developers decided to replace sysvinit with systemd. Besides, it was atrocious when developers began horsing around with the crucial Linux subsystems. But, if the developers do not like a particular software application, desktop environment, or command, they shift their attention toward its alternatives.
Moreover, since subsystems have deep hooks in the kernel, every management script, and software package dependencies, it is pretty effortless to replace the older version.
In short, things change. Computers are getting more complex and changing the software to meet up with the system requirement is very essential.
systemctl – Controls the systemd system and services.
hostnamectl – Controls the hostname.
timedatectl – Sets the time and date.
systemadm – Acts as a front end for the systemctl command.
systemd-cgls – Shows cgroup contents.
localectl- Configures the system’s local and keyword layout.
journalctl – Manages the journal and systemd’s own logging system.
Advantages of Systemd
Since 2014, several Linux distributions are relying on the Systemd system and service manager. It provides them with the following advantages:
Systemd daemons make the process of supervising and controlling processes effortless. Besides, it also makes the execution of parallelized jobs seamless.
Provides systemctl command and cgroups to make life easier.
The systemctl utility provides the administrator with more detailed error messages, including runtime errors and startup errors.
cGroups, also known as ‘Control Groups,’ enables you to group the processes into the hierarchy for effortless management.
As seen, it is a straightforward process to check process attributes such as function and property. For example, in the Systemd framework, child processes become “children” and are organized in the appropriate “parent” collection to show inheritance.
Please continue reading How To Use Systemctl to Manage Systemd Services and Units.
Unit files are the primary object in systemd. In turn, that system knows how to operate on and manage. It is a plain text ini-style file that encodes information about a service, a socket, a device, a mount point, an automount point, and a swap file. In this section, we will show you how to manage Units using the systemctl command.
To list all unit types, run the following command:
systemctl -t help
You will get the following list:
Available unit types:
service
socket
target
device
mount
automount
swap
timer
path
slice
scope
After that, to get a list of all installed units, run the following command:
Following, to list all running units, use the following command:
systemctl list-units
You should see the following output:
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active running Arbitrary Executable File
dev-fuse.device loaded activating tentative /dev/fuse
sys-devices-pci0000:00-0000:00:01.0-backlight-amdgpu_bl0.device loaded active plugged /sys/devices/pci0000:00/00
sys-devices-pci0000:00-0000:00:01.1-sound-card0.device loaded active plugged /sys/devices/pci0000:00/00
sys-devices-pci0000:00-0000:00:02.4-0000:14:00.0-net-enp20s0.device loaded active plugged RTL810xE PCI Express Fast
sys-devices-pci0000:00-0000:00:02.5-0000:16:00.0-net-wlp22s0.device loaded active plugged QCA9377 802.11ac Wireless
sys-devices-pci0000:00-0000:00:09.2-sound-card1.device loaded active plugged /sys/devices/pci0000:00/00
sys-devices-pci0000:00-0000:00:10.0-usb2-2\x2d1-2\x2d1:1.0-bluetooth-hci0.device loaded active plugged /sys/devices/pci0000:00/00
sys-devices-pci0000:00-0000:00:11.0-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda1.device loaded active plugged TOSHIBA_MQ01ABD100 ESP
sys-devices-pci0000:00-0000:00:11.0-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda2.device loaded active plugged TOSHIBA_MQ01ABD100 Micr
sys-devices-pci0000:00-0000:00:11.0-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda3.device loaded active plugged TOSHIBA_MQ01ABD100 OS
sys-devices-pci0000:00-0000:00:11.0-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda4.device loaded active plugged TOSHIBA_MQ01ABD100 4
sys-devices-pci0000:00-0000:00:11.0-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda5.device loaded active plugged TOSHIBA_MQ01ABD100 5
sys-devices-pci0000:00-0000:00:11.0-ata1-host0-target0:0:0-0:0:0:0-block-sda.device loaded active plugged TOSHIBA_MQ01ABD100
sys-devices-platform-serial8250-tty-ttyS0.device loaded active plugged /sys/devices/platform/seri
sys-devices-platform-serial8250-tty-ttyS1.device loaded active plugged /sys/devices/platform/seri
sys-devices-platform-serial8250-tty-ttyS10.device loaded active plugged /sys/devices/platform/seri
Then, to list all failed units, run the following command:
systemctl --failed
Result is the following output:
UNIT LOAD ACTIVE SUB DESCRIPTION
● isc-dhcp-server.service loaded failed failed ISC DHCP IPv4 server
● isc-dhcp-server6.service loaded failed failed ISC DHCP IPv6 server
● mariadb.service loaded failed failed MariaDB 10.1.48 database server
● nginx.service loaded failed failed A high performance web server and a reverse proxy server
● systemd-backlight@backlight:acpi_video0.service loaded failed failed Load/Save Screen Backlight Brightness of backlight:acpi_video0
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.
5 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
If you want to list only service unit files, run the following command:
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.
If you want to display the content of specific unit file, run the following comand:
systemctl cat nginx.service
Now, this is the following output:
# /lib/systemd/system/nginx.service
# Stop dance for nginx
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
To see a unit’s dependency tree, run the following output:
systemctl list-dependencies nginx.service
Get a list of all dependent service units in the following output:
Secondly, the main purpose of the systemd is to manage services and daemons for the server at any point while the system is running. Basic syntex to manage services is shown below:
Evidently, using systemd, you can also mount, unmount, remount, reload system mount points and also check the status of mount points. Forthwith, to get a list of all system mount points, run the following command:
systemctl list-unit-files --type=mount
You should see the following list:
UNIT FILE STATE
-.mount generated
boot-efi.mount generated
Data.mount generated
dev-hugepages.mount static
dev-mqueue.mount static
proc-fs-nfsd.mount static
proc-sys-fs-binfmt_misc.mount static
run-rpc_pipefs.mount static
From the above list, start ad stop the Data.mount using the following command:
How To Use Systemctl to Manage Systemd Services and Units Conclusion
In this guide, we explained systemd units in more detail and how to use the systemctl command to explore and manage units. We also show you how to manage services, mounts and sockets using the systemctl command. The systemctl utility is very useful for system administrator for service and system state management.
In order to read more of our content about Linux click over here.
I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.