Systemd socket units
These are the notes of a training course on systemd I gave as part of my work with Truelite.
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
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 email@example.com:55555-127.0.0.1:36936.service loaded active running Run ddate as a network service (127.0.0.1:36936) firstname.lastname@example.org: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
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 ├─email@example.com:55555-127.0.0.1:36936.service │ ├─18214 /bin/sh -ec while true; do /usr/bin/ddate; sleep 1m; done │ └─18661 sleep 1m └─firstname.lastname@example.org: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.