<div dir="ltr"><div><font face="times new roman, serif">I want to have a service with several instances, each of which has a configuration file; when configuration files appear and disappear, I want the corresponding instances to be created and started, and die, respectively, and in particular have the running processes corresponding to the removed units be killed.<br></font></div><font face="times new roman, serif"><br>I can get them to start the first time, but they do not stop when configuration files change. If I</font><font face="monospace, monospace"> systemctl daemon-reload</font><font face="times new roman, serif">, the links in</font><font face="monospace, monospace"> /run/systemd/system-generators</font><font face="times new roman, serif"> get properly adjusted, but I cannot figure out how to tell my service to kill the no-longer-needed processes. If I also </font><font face="monospace, monospace">systemctl reload</font><font face="times new roman, serif"> the service, all existing processes are killed, which is absolutely not what I want.</font><div><font face="times new roman, serif"><br></font></div><div><font face="times new roman, serif"><span style="color:rgb(34,34,34);font-family:"times new roman",serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">Here is what I've done: w</span>hat am I doing wrong?<br><br>I tried modelling this after what OpenVPN does:<br></font><br><span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"># <b>cat /etc/systemd/system/letters.service</b>
</span><br>[Unit]
<br>Description=Letters service
<br>After=network.target
<br>
<br>[Service]
<br>Type=oneshot
<br>RemainAfterExit=yes
<br>ExecStart=/bin/true
<br>ExecReload=/bin/true
<br>WorkingDirectory=/var/tmp/letters
<br>
<br>[Install]
<br>WantedBy=multi-user.target<br></span><br><span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"># <b>cat /etc/systemd/system/letters@.service</b>
</span><br>[Unit]
<br>Description=Letters for %i
<br>PartOf=letters.service
<br>ReloadPropagatedFrom=letters.service
<br>Before=systemd-user-sessions.service
<br>After=syslog.target network-online.target
<br>Wants=network-online.target
<br>
<br>[Service]
<br>Type=simple
<br>PrivateTmp=false
<br>WorkingDirectory=/var/tmp/letters
<br>ExecStart=/usr/local/bin/letter --daemon --pidfile /run/letters/%i.pid
<br>PIDFile=/run/letters/%i.pid
<br>ExecReload=/bin/kill -HUP $MAINPID
<br>DeviceAllow=/dev/null rw
<br>ProtectSystem=true
<br>ProtectHome=true
<br>
<br>[Install]
<br>WantedBy=multi-user.target<br></span><br><span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"># <b>cat /etc/systemd/system-generators/letters</b>
</span><br>#!/bin/sh
<br>exec 2>> /tmp/opg2
<br>set -eux
<br>GENDIR="$1"
<br>WANTDIR="$1/letters.service.wants"
<br>SERVICEFILE="/lib/systemd/system/letters@.service"
<br>CONFIG_DIR=/var/letters/
<br>mkdir -p "$WANTDIR"
<br>for CONFIG in `cd $CONFIG_DIR; ls *.conf 2> /dev/null`; do
<br> NAME=${CONFIG%%.conf}
<br> ln -s "$SERVICEFILE" "$WANTDIR/letters@$NAME.service"
<br>done
<br>exit 0<br></span><br><br><font face="times new roman, serif">Now for the fun:</font><div><font face="times new roman, serif"><br></font></div><div><font face="times new roman, serif"><span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"># <b>touch /var/letters/aleph.conf /var/letters/e.conf /var/letters/zeta.conf</b></span></span></font></div><div><font face="times new roman, serif"><span style="font-family:monospace"><font color="#000000"><span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"># </span><span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">systemctl daemon-reload
</span><br><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"># </span><span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">ls -l /run/systemd/generator/letters.service.wants
</span><br>total 0
<br>lrwxrwxrwx 1 root root 36 Mar 19 17:37 letters@aleph.service -> /lib/systemd/system/letters@.service
<br>lrwxrwxrwx 1 root root 36 Mar 19 17:37 letters@e.service -> /lib/systemd/system/letters@.service
<br>lrwxrwxrwx 1 root root 36 Mar 19 17:37 letters@zeta.service -> /lib/systemd/system/letters@.service<br></span></font></span><span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"># <b>systemctl enable letters.service</b>
</span><br>Created symlink /etc/systemd/system/multi-user.target.wants/letters.service → /etc/systemd/system/letters.service.<br></span><span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"># <b>systemctl start letters.service</b>
<br></span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"># <b>ps ax | grep lett</b>
</span><br>22853 ? S 0:00 /usr/local/bin/<span style="font-weight:bold;color:rgb(255,84,84);background-color:rgb(255,255,255)">lett</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">er --daemon --pidfile /run/</span><span style="font-weight:bold;color:rgb(255,84,84);background-color:rgb(255,255,255)">lett</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">ers/e.pid
</span><br>22927 ? S 0:00 /usr/local/bin/<span style="font-weight:bold;color:rgb(255,84,84);background-color:rgb(255,255,255)">lett</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">er --daemon --pidfile /run/</span><span style="font-weight:bold;color:rgb(255,84,84);background-color:rgb(255,255,255)">lett</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">ers/aleph.pid
</span><br>22928 ? S 0:00 /usr/local/bin/<span style="font-weight:bold;color:rgb(255,84,84);background-color:rgb(255,255,255)">lett</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">er --daemon --pidfile /run/</span><span style="font-weight:bold;color:rgb(255,84,84);background-color:rgb(255,255,255)">lett</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">ers/zeta.pid</span><br></span><br></font></div><div><font face="times new roman, serif">So far so good. This is working as expected. Even better:</font></div><div><br></div><div><span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"># <b>touch /var/letters/tcha.conf</b></span><br></span><span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"># <b>systemctl daemon-reload</b></span><br></span><span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"># <b>systemctl start letters.service</b></span><br></span><span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"># <b>ps ax | grep lett</b>
</span><br>22853 ? S 0:00 /usr/local/bin/<span style="font-weight:bold;color:rgb(255,84,84);background-color:rgb(255,255,255)">lett</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">er --daemon --pidfile /run/</span><span style="font-weight:bold;color:rgb(255,84,84);background-color:rgb(255,255,255)">lett</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">ers/e.pid
</span><br>22927 ? S 0:00 /usr/local/bin/<span style="font-weight:bold;color:rgb(255,84,84);background-color:rgb(255,255,255)">lett</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">er --daemon --pidfile /run/</span><span style="font-weight:bold;color:rgb(255,84,84);background-color:rgb(255,255,255)">lett</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">ers/aleph.pid
</span><br>22928 ? S 0:00 /usr/local/bin/<span style="font-weight:bold;color:rgb(255,84,84);background-color:rgb(255,255,255)">lett</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">er --daemon --pidfile /run/</span><span style="font-weight:bold;color:rgb(255,84,84);background-color:rgb(255,255,255)">lett</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">ers/zeta.pid
</span><br>23083 ? S 0:00 /usr/local/bin/<span style="font-weight:bold;color:rgb(255,84,84);background-color:rgb(255,255,255)">lett</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">er --daemon --pidfile /run/</span><span style="font-weight:bold;color:rgb(255,84,84);background-color:rgb(255,255,255)">lett</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">ers/tcha.pid
</span><br></span><br></div><div><font face="times new roman, serif">But now the trouble begins:</font></div><div><br></div><div><span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"># <b>rm /var/letters/e.conf</b>
<br></span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"># <b>systemctl daemon-reload
</b></span><br><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"># <b>ls -l /run/systemd/generator/letters.service.wants</b>
</span><br>total 0
<br>lrwxrwxrwx 1 root root 36 Mar 19 17:44 letters@aleph.service -> /lib/systemd/system/letters@.service
<br>lrwxrwxrwx 1 root root 36 Mar 19 17:44 letters@tcha.service -> /lib/systemd/system/letters@.service
<br>lrwxrwxrwx 1 root root 36 Mar 19 17:44 letters@zeta.service -> /lib/systemd/system/letters@.service<br></span><br></div><div><span style="color:rgb(34,34,34);font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"><span style="color:rgb(34,34,34);font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"><font face="times new roman, serif">The link </font></span><font face="monospace">letters@e.service </font></span><font face="times new roman, serif">has been correctly removed, but the corresponding process still exists:</font></div><div><font face="times new roman, serif"><span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"># <b>ps ax | grep e.pid</b>
</span><br>22853 ? S 0:00 /usr/local/bin/letter --daemon --pidfile /run/letters/<span style="font-weight:bold;color:rgb(255,84,84);background-color:rgb(255,255,255)">e.pid</span><br></span></font></div><div><font face="times new roman, serif"><span style="font-family:monospace"><span style="font-weight:bold;color:rgb(255,84,84);background-color:rgb(255,255,255)"><br></span></span></font></div><br><div><br></div></div></div>