0

I have a complex systemd target unit with a number of dependency units. I would like to list and parse the status of these without falling back to a loop.

the 'list-dependencies' sub-command only gives the status in form of a (coloured, not reproduced in plain unicode) dot, that is not really good to parse.

> systemctl list-dependencies --no-page myunit.target
dcache.target
● ├─mytemplate@unit_foo.service
● ├─mytemplate@unit_bar.service
● ├─mytemplate@unit_baz.service
...

the --no-page flag seems to have no affect and the output stays the same with/without.

Alternatively,

systemctl list-dependencies --plain myunit.target

only list the dependencies but without their current status

Is there a way to generate output from 'systemctl list-dependencies', that can be parsed or is in JSON or similar?

2

Systemd doesn't support any kind of json api out of the box. Check the discussion below: https://github.com/systemd/systemd/issues/83. The only way to get machine readable output here is the --plain version, which you mentioned and works absolutely fine. The status information you can get with systemctl is-active. So you need to do a little bit of scripting to get what you want here. For your specific problem you could just use:

systemctl list-dependencies --plain nginx | xargs -I {} sh -c 'echo -n {}": "; systemctl is-active {};'
  • thanks - actually, I was hoping to avoid loops/pipes as the information is somewhat already present and encoded in the dots (for humans), but apparently parsing was no primary design goal here. Anyway, I have noticed, that the dependency 'status dot' is no sure sign, that my sub-units are active and running, so that I definitifly have to script. – THX Sep 15 '20 at 8:27

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.