The [Unit] section describes the service, specifies the ordering dependencies, as well as conflicting units. In [Service], a sequence of custom scripts is specified to be executed during unit activation, on stop, and on reload. Finally, the [Install] section lists units that depend on the service.
-
You can view the cgroup of a service with the systemctl status <service_name>
command.
Let’s view the status of our custom system service shellscript.service
again.
$ systemctl status shellscript.service
You get an output similar to the following
● shellscript.service - My Shell Script
Loaded: loaded (/lib/systemd/system/shellscript.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2022-10-23 19:39:02 +04; 1s ago
Main PID: 6821 (script.sh)
Tasks: 4 (limit: 9415)
Memory: 1.0M
CPU: 1.945s
CGroup: /system.slice/shellscript.service
├─6821 /bin/bash /usr/bin/script.sh
├─6822 /bin/bash /usr/bin/script.sh
├─6824 nc -l -k -p 8080 -q 1
└─6825 dd if=/dev/zero of=/dev/null
The output shows that shellscript.service
is under the system.slice
control group.
The second line 6821 /bin/bash /usr/bin/script.sh
shows the process ID and the command used to start shellscript.service
.
Subsequent lines under the CGroup
are the other commands executed in the service.
-
View your system’s cgroup hierarchy
$ systemctl status
-
You can view the system resource usage by each cgroup
$ systemd-cgtop
-
Slices allow one to create a hierarchical structure in which relative shares of resources are defined for the entities that belong to those slices.
View a list of all systemd slices
$ systemctl -t slice --all
-
Create a systemd slice at /etc/systemd/system/testslice.slice
. Add the following to the file.
[Unit]
Description=Custom systemd slice for SNA lab on systemd.
Before=slices.target
[Slice]
MemoryAccounting=true
CPUAccounting=true
MemoryMax=10%
CPUQuota=10%
This slice will set CPU and memory usage limit to all processes running under it. As seen in the configuration, a maximum of 10% of memory and CPU resources can be used by the processes running under this control group testslice.slice
.
-
Let’s add our custom service shellscript.service
to this new control group.
Modify the service file /lib/systemd/system/shellscript.service
to use this systemd slice by adding the line Slice=testslice.slice
to the [Service]
section as shown below:
[Unit]
Description=My custom web service to show system processes
[Service]
ExecStart=/usr/bin/script.sh
Slice=testslice.slice
[Install]
WantedBy=multi-user.target
-
Reload the daemon and restart the service to apply the changes
$ sudo systemctl daemon-reload
$ sudo systemctl restart shellscript.service
-
Refresh the web page. The output shows that dd
now uses less than 10% of the CPU.
-
Run $ systemd-cgtop
to view resource usage by cgroups.
Control Group Tasks %CPU Memory Input/s Output/s
/ 774 13,9 1.7G - -
testslice.slice 2 9,9 956.0K - -
testslice.slice/shellscript.service 2 9,9 576.0K - -
user.slice 489 3,1 1.5G - -
user.slice/user-1000.slice 489 3,4 1.3G - -
-
View the hierarchy and other artifacts about the cgroup.
$ systemctl -t slice --all
$ systemctl status
-
View systemd log for your service
$ journalctl -u shellscript.service
Lab 8: Systemd
Task 1: Create a shell script
top
command.You have to manually run this script whenever the system is restarted. We can solve this by creating a systemd service for it. This will give us more options for managing the script’s execution.
Task 2: Create a systemd file
Next, create a systemd service file for the script on your system. This file must have
.service
extension and saved under the/lib/systemd/system/
directoryNow, add the following content and update the script filename and location. You can also change the description of the service. After that, save the file and close it.
Task 3: Enable the service
*.service
file.dd
process shown in the output was executed by the service we just created. This process is using 100% of the CPU. We can fix this problem by making use of systemd control groups.Task 4: Systemd control groups (cgroups)
Systemd control group is a mechanism that allows you to control the use of system resources by a group of processes.
You can view the cgroup of a service with the
systemctl status <service_name>
command.Let’s view the status of our custom system service
shellscript.service
again.You get an output similar to the following
The output shows that
shellscript.service
is under thesystem.slice
control group.The second line
6821 /bin/bash /usr/bin/script.sh
shows the process ID and the command used to startshellscript.service
.Subsequent lines under the
CGroup
are the other commands executed in the service.View your system’s cgroup hierarchy
You can view the system resource usage by each cgroup
Slices allow one to create a hierarchical structure in which relative shares of resources are defined for the entities that belong to those slices.
View a list of all systemd slices
Create a systemd slice at
/etc/systemd/system/testslice.slice
. Add the following to the file.This slice will set CPU and memory usage limit to all processes running under it. As seen in the configuration, a maximum of 10% of memory and CPU resources can be used by the processes running under this control group
testslice.slice
.Let’s add our custom service
shellscript.service
to this new control group.Modify the service file
/lib/systemd/system/shellscript.service
to use this systemd slice by adding the lineSlice=testslice.slice
to the[Service]
section as shown below:Reload the daemon and restart the service to apply the changes
Refresh the web page. The output shows that
dd
now uses less than 10% of the CPU.Run
$ systemd-cgtop
to view resource usage by cgroups.View the hierarchy and other artifacts about the cgroup.
View systemd log for your service
Questions to answer
Show the following boot-up performance statistics on your system:
Take the systemd unit
graphical.target
as your starting point, start tracing backwards using only theRequires
variable. At what systemd unit do you reach a dead end where there is no moreRequires
variable?Wants
some systemd units. Why does it want these units?Create a simple web server in bash that shows the following: system uptime, inode usage, current memory, disk space usage statistics, and the last 15 lines of
/var/log/syslog
.Create a systemd service that will update your package sources list from the repository.
Bonus
/etc/systemd/system/<your_target>.target
./etc/systemd/system/<your_target>.wants/
/usr/lib/systemd/system/
that you wish to enable.