System Administration & Network Administration
systemd dependencies systemctl
Updated Mon, 22 Aug 2022 01:06:40 GMT

listing Systemd target unit's dependencies in a machine readable format


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?




Solution

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 {};'




Comments (1)

  • +0 – 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. — Sep 15, 2020 at 08:27  


External Links

External links referenced by this document: