5

I'd like to list all enabled service instances.

The following lists enabled services, but only shows the service, and not individual instance:

systemctl list-unit-files --state=enabled

The following lists all running instances:

systemctl list-units --state=running

I would expect something like the following to show enabled instances:

systemctl list-units --state=enabled

But that does not work.

So, if I start two service instances with:

systemctl start foo-service@primary
systemctl start foo-service@secondary

...but then I only enable one:

systemctl enable foo-service@secondary

The only way I've been able to find out which instances are enabled is with:

ls /etc/systemd/system/multi-user.target.wants/

But that seems very kludge-y. Is there a systemd way of doing this? Version is 237 if it matters.

2 Answers2

1

I'm pretty sure you're looking for systemctl list-units --state=loaded.

Michael Hampton
  • 252,907
0

As a variant of https://serverfault.com/a/940990/407952 you could specify the service and parse the output (for active instances) using awk like this:

systemctl list-units -q --state=loaded foo-service@* | awk '$3 == "active" { print $1 }'

Note that the output is formatted like this, so $3is ACTIVE:

UNIT                  LOAD   ACTIVE SUB     DESCRIPTION

For enabled instances the following two-stage check worked for me:

for instance in $(systemctl list-units -q --state=inactive --state=active foo-service@* |
awk '$1 == "UNIT" { OK = 1; next } /^$/ { exit } OK && NF >= 4 { print $1 } ')
do
    systemctl -q is-enabled $instance && echo $instance
done

This lists the enabled instance services. Specifying both states was necessary (for systemd-228) to list units that are enabled, but stopped.

U. Windl
  • 478