These are the notes of a training course on systemd I gave as part of my work with Truelite.
.socket
units
Socket units tell systemd to listen on a given IPC, network socket, or file system FIFO, and use another unit to service requests to it.
For example, this creates a network service that listens on port 55555:
# /etc/systemd/system/ddate.socket
[Unit]
Description=ddate service on port 55555
[Socket]
ListenStream=55555
Accept=true
[Install]
WantedBy=sockets.target
# /etc/systemd/system/ddate@.service
[Unit]
Description=Run ddate as a network service
[Service]
Type=simple
ExecStart=/bin/sh -ec 'while true; do /usr/bin/ddate; sleep 1m; done'
StandardOutput=socket
StandardError=journal
Note that the .service
file is called ddate@
instead of ddate
: units
whose name ends in '@' are template units
which can be activated multiple times, by adding any string after the '@' in
the unit name.
If I run nc localhost 55555
a couple of times, and then check the list of
running units, I see ddate@…
instantiated twice, adding the local and remote
socket endpoints to the unit name:
$ systemctl list-units 'ddate@*'
UNIT LOAD ACTIVE SUB DESCRIPTION
ddate@15-127.0.0.1:55555-127.0.0.1:36936.service loaded active running Run ddate as a network service (127.0.0.1:36936)
ddate@16-127.0.0.1:55555-127.0.0.1:37002.service loaded active running Run ddate as a network service (127.0.0.1:37002)
This allows me to monitor each running service individually.
systemd also automatically creates a slice
unit
called system-ddate.slice
grouping all services together:
$ systemctl status system-ddate.slice
● system-ddate.slice
Loaded: loaded
Active: active since Thu 2017-09-21 14:25:02 CEST; 9min ago
Tasks: 4
CGroup: /system.slice/system-ddate.slice
├─ddate@15-127.0.0.1:55555-127.0.0.1:36936.service
│ ├─18214 /bin/sh -ec while true; do /usr/bin/ddate; sleep 1m; done
│ └─18661 sleep 1m
└─ddate@16-127.0.0.1:55555-127.0.0.1:37002.service
├─18228 /bin/sh -ec while true; do /usr/bin/ddate; sleep 1m; done
└─18670 sleep 1m
This allows to also work with all running services for this template unit as a whole, sending a signal to all their processes and setting up resource control features for the service as a whole.
See: