<div dir="ltr"><div>Hi All</div><div><br></div><div>I have a fairly complex (at least to me) setup of a master target spawning multiple services and groups of instance services that are chained in a specific order. I use systemd to manage all of the sockets that allow data to flow between these different stages.<br></div><div></div><div>I use a master Target (foo.target) defined to manage the services state, so I can easily stop and restart everything.</div><div></div><div>The first service (bar.service) is oneshot script that starts multiple groups of instance services (the number of spawned services depends on CPU cores and queue sizes among other things). I have ExecStart and ExecStop scripts in the unit file.<br></div><div></div>For example: bar.service - This is the oneshot that spawns "n" baz@.service and "n" qux@.service.  There are a lot of dependencies and so far systemd has done everything I need. <br><br><div></div><div>What do I want?</div><div>If there is any failure or issue with any of the child processes spawned from any of the instance units then I would like the whole fragile house of cards to be torn down and restarted. i.e. the whole foo.target system state to be restarted, not just the individual instance service (and subsequent process) itself.</div><div><br></div><div>What are my observations?</div><div>This all works well except for the instance units. When I include the instance units into the "BindsTo" with the target, I get additional processes and services launched that I do not expect.</div><div><br></div><div>I have simplified the whole thing to two services, a target and a very simple script. This demonstrates exactly the same thing that I see in the much more complex version.<br></div><div><br></div><div>The "master service". This is a oneshot that spawns the instance units.<br></div><div>foo.service</div><div><span style="font-family:monospace">[Unit]<br>Description=Foo service<br>BindsTo=foo.target<br>[Service]<br>Type=oneshot<br>ExecStart=some-path-somewhere/foo-start.sh<br>[Install]<br>WantedBy=foo.target</span></div><div><span style="font-family:arial,sans-serif"><br></span></div><div><span style="font-family:arial,sans-serif">The instance unit that does the actual work. In this case we have a placeholder to show the problem. In my real example I have a long chain of services like this that uses systemd managed sockets to pass data along.<br></span></div><div><span style="font-family:monospace">bar@.service<br></span></div><div><span style="font-family:monospace">[Unit]<br>Description=Test service Bar instance %i<br>BindsTo=foo.target<br>[Service]<br>Type=simple<br>ExecStart=sh -c 'while true; do echo Bar %i is alive; sleep 3; done'<br>[Install]<br>WantedBy=foo.target</span><br></div><div><span style="font-family:monospace"><br></span></div><div></div><div>The target that allows me to stop and restart everything easily:</div><div><span style="font-family:monospace">[Unit]<br>Description=Test Services<br>Requires=foo.service<br>[Install]<br>WantedBy=multi-user.target</span></div><div><br></div><div><div><br></div><div>And finally the script called in foo.service:</div><div>foo-start.sh</div><div><span style="font-family:monospace">#!/usr/bin/bash<br>num=4<br>eval systemctl start bar@{1..${num}}.service</span></div></div><div><br></div><div>In order to tightly couple the processes and services I use BindsTo. But I am getting inconsistent behavior when trying to apply this to the instance units from the target.<br></div><div><br></div><div><span style="font-family:arial,sans-serif">Scenario A:<br></span></div><div><span style="font-family:arial,sans-serif">WITHOUT BindsTo for the instance units in the target:</span></div><div><span style="font-family:arial,sans-serif">- Everything stops and starts with the target. <br></span></div><div><span style="font-family:arial,sans-serif">- I get the correct number of processes.</span></div><div><span style="font-family:arial,sans-serif">- If I kill one of the PIDs below, systemd only restarts that process - which of course is what most use-cases would require.<br></span></div><div><span style="font-family:monospace"># ps -ef | grep [Bb]ar<br></span></div><div><span style="font-family:monospace">root       17878       1  0 15:25 ?        00:00:00 sh -c while true; do echo Bar 1 is alive; sleep 3; done<br>root       17880       1  0 15:25 ?        00:00:00 sh -c while true; do echo Bar 2 is alive; sleep 3; done<br>root       17882       1  0 15:25 ?        00:00:00 sh -c while true; do echo Bar 3 is alive; sleep 3; done<br>root       17887       1  0 15:25 ?        00:00:00 sh -c while true; do echo Bar 4 is alive; sleep 3; done</span></div><div><br></div><div><span style="font-family:monospace"> # systemctl list-units bar@\*.service<br>  UNIT            LOAD   ACTIVE SUB     DESCRIPTION<br>  bar@1.service   loaded active running Test service Bar instance 1<br>  bar@2.service   loaded active running Test service Bar instance 2<br>  bar@3.service   loaded active running Test service Bar instance 3<br>  bar@4.service   loaded active running Test service Bar instance 4</span></div><div><br></div><div>Scenario B:</div><div>WITH BindsTo in the unit instance file (<span style="font-family:monospace">BindsTo=bar@%i.service </span><span style="font-family:monospace">or </span><span style="font-family:monospace">BindsTo=bar@%N.service):</span></div><div>- Everything stops and start with the target.</div><div>- i get EXTRA PROCESSES.</div><div>- If I kill one of the PIDs below, everything restarts properly (i.e. the whole target) and I get the behavior I am looking for.<br></div><div><span style="font-family:monospace">root       29250       1  0 16:08 ?        00:00:00 sh -c while true; do echo Bar foo is alive; sleep 3; done  #<<<< What's this guy doing here?<br>root       29256       1  0 16:08 ?        00:00:00 sh -c while true; do echo Bar 1 is alive; sleep 3; done<br>root       29258       1  0 16:08 ?        00:00:00 sh -c while true; do echo Bar 2 is alive; sleep 3; done<br>root       29260       1  0 16:08 ?        00:00:00 sh -c while true; do echo Bar 3 is alive; sleep 3; done<br>root       29262       1  0 16:08 ?        00:00:00 sh -c while true; do echo Bar 4 is alive; sleep 3; done</span></div><div></div><div><br></div><div>So, however systemd is expanding the variables %i or %N, it's including an additional service.</div><div><br></div><div><span style="font-family:monospace"> # systemctl list-units bar@\*.service<br>  UNIT            LOAD   ACTIVE SUB     DESCRIPTION<br>  bar@1.service   loaded active running Test service Bar instance 1<br>  bar@2.service   loaded active running Test service Bar instance 2<br>  bar@3.service   loaded active running Test service Bar instance 3<br>  bar@4.service   loaded active running Test service Bar instance 4<br>  bar@foo.service loaded active running Test service Bar instance foo   #<<<< Here he is again! ????<br></span></div><div><br></div><div>Does anyone have any suggestions?  Is there a more elegant way to connect the processes to the whole target for restart purposes? <br></div><div>Maybe this is a bug in my version of systemd but more likely I'm doing something wrong. <br></div><div><br></div><div>Version info:</div><div># systemctl --version<br>systemd 247 (247.3-7+deb11u1)<br>+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +ZSTD +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=unified</div><div><br></div><div>Thank you for reading this far and thank you also in advance for any suggestions.</div><div><br></div><div>Cheers!<br></div><div><br></div></div>