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 232 if it matters.

2
  • You should mention the version of system you are using. Oct 10, 2017 at 15:03
  • This question is more or less identical to a question on serverfault.com May 16, 2021 at 15:32

2 Answers 2

3

One solution I found was to list all of the units (which will include all instances of a templated service), and use xargs to run a command for each unit (checking whether each one is enabled):

$ systemctl list-units --no-legend | awk '{print $1}' | xargs -I % sh -c 'echo "%: $(systemctl is-enabled %)"'
auditd.service: enabled
chronyd.service: enabled
crond.service: enabled
...

To explain what is going on here:

  • systemctl list-units --no-legend : List all systemd units, without showing the headers at the top of the ouput

  • awk '{print $1}' : Print the first part of each line, which is the name of the unit

  • xargs -I % sh -c 'echo "%: $(systemctl is-enabled %)"' : Runs a command for line of the piped input. This will echo the unit name, followed by the output of systemctl is-enabled <service>.

1
  • 1
    Thank you for this. I was able to adapt it to only output enabled instances like so: systemctl list-units --plain --no-legend my@\*.service | awk '{print $1}' | xargs -r -I % sh -c 'systemctl is-enabled % > /dev/null && echo %' sh. I added the filter my@\*.service because I only cared about instances of one particular service.
    – Walf
    Jul 23, 2020 at 9:43
-1

You can use 'grep' to search for and list the enabled services

systemctl list-unit-files | grep enabled
1
  • 4
    As mentioned in the question, list-unit-files only shows enabled services, not enabled instances.
    – rich remer
    Oct 10, 2017 at 19:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .