1

Systemd allows you to create template units, as documented in systemd.unit.

There are a number of variables you can use in your template unit.

I'm interested in "%j":

This is the string between the last "-" and the end of the prefix name.

The "prefix name" is also defined:

For instantiated units, this refers to the string before the first "@" character of the unit name.

I'm clear what they are, not clear why they exist.

I'm guessing perhaps they are running multiple versions of the same service. What's a real-world example of how this is used?

2

Units can have additional settings in .d/ directories alongside the unit. For example, foo.service can be extended via foo.service.d/*.conf.

Template units will use two directories – instance and template, so getty@ttyS1.service will be extended from both getty@ttyS1.service.d/*.conf and getty@.service.d/*.conf. This way you can extend all instances of the unit at once. In both cases, you the unit and its extension configs may use %i to get the "ttyS1" bit.

However, some units cannot use templates, e.g. slices (representing cgroups) are named user-<UID>.slice and not user@<UID>.slice because these units' name represents a filesystem path (the dash is mapped to a slash, and therefore "user-123.slice" is a child of "user.slice").

Because it is desired to be able to configure all individual user slices (e.g. give each slice x% of memory), a similar mechanism was added for units which use path-like names: similarly to the getty example above, the unit user-1000.slice can be extended from both user-1000.slice.d/ and user-.slice.d/, with files in the latter generic directory being able to use %j to get the "1000" bit.

This last example is sort of used in practice by the default systemd installation:

$ systemctl cat user-1000.slice
# /usr/lib/systemd/system/user-.slice.d/10-defaults.conf
[Unit]
Description=User Slice of UID %j
  • Both answers were good, but I found this one clearer. Thanks! – Mark Stosberg Apr 24 at 12:14
1

One example of this might be systems units that refer to paths in the name. In such units, / is replaced by - (and the leading - is dropped). For example, a mount unit for /home/muru would be named home-muru.mount. Similarly, I can see other path-based unit names when I run systemctl list-units:

sys-devices-platform-soc-3f980000.usb-usb1-1\x2d1-1\x2d1.5-1\x2d1.5:1.0-host0-target0:0:0-0:0:0:0>
sys-devices-platform-soc-3f980000.usb-usb1-1\x2d1-1\x2d1.5-1\x2d1.5:1.0-host0-target0:0:0-0:0:0:0>
sys-devices-virtual-misc-rfkill.device          loaded active plugged   /sys/devices/virtual/misc
sys-devices-virtual-tty-ttyprintk.device        loaded active plugged   /sys/devices/virtual/tty/
sys-module-configfs.device                      loaded active plugged   /sys/module/configfs
sys-subsystem-net-devices-eth0.device           loaded active plugged   SMSC9512/9514 Fast Ethern
sys-subsystem-net-devices-wlan0.device          loaded active plugged   /sys/subsystem/net/device

In many of these, the last component of the path, which would be the part given by %j, might be useful within the unit. I don't know if any of these actually use the name, but it might be convenient, for example, to use %j to get the username for a generator of mount units for directories in /home, or in /run/user, or similar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.