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.

  • You should mention the version of system you are using. – Mark Stosberg Oct 10 '17 at 15:03
  • This question is more or less identical to a question on serverfault.com – Erik Sjölund yesterday

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
    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 '20 at 9:43

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

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

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.