Re: [systemd-devel] Starting one service when another one starts
On Mo, 11.04.22 12:41, Nick Howitt (n...@howitts.co.uk) wrote: > > You can add WantedBy=siad.service to [Install] section of > > clearshare-scheduler.service. In general you can always extend Wants by > > manually creating necessary links. This does not require you to edit > > unit definition itself. You can also create drop-in (although I am not > > sure whether they are already supported in your systemd version). > > I've tried this and can implement what you've described, but as you say, it > does not help me when starting the WantedBy service (siad). You must issue "systemctl enable" to actually make the stuff from [Install] apply. Lennart -- Lennart Poettering, Berlin
Re: [systemd-devel] Starting one service when another one starts
On Fr, 08.04.22 12:54, Nick Howitt (n...@howitts.co.uk) wrote: > Hi, > I apologise if this is not the right place for user help. If it is not, > please point me to the best place. > > I am trying to start a service (clearshare-scheduler) when another service > (siad) starts. Clearshare-scheduler is an odd service. When you start it it > may run for ages (days+) or it may terminate immediately so I have set it up > as a oneshot: That's not really necessary. You can set RemainAfterExit=yes to define units if the other types which will remain in "running" state even if all processes they define die. > > [Unit] > Description=Clearshare Scheduler > PartOf=siad.service > After=siad.service > > [Service] > Type=oneshot > Environment="TERM=dumb" > ExecStartPre=-/usr/bin/killall -15 -q > /usr/sbin/clearshare-scheduler.sh I hope this is for testing only.. > ExecStartPre=-/usr/bin/echo "$(/usr/bin/date) Starting scheduler from > systemd" >> /var/log/scheduler.log systemd does not implement a shell, and ">>" is shell syntax, not systemd syntax. > ExecStart=/usr/sbin/clearshare-scheduler.sh > /dev/null Similar. > ExecStop=-/usr/bin/killall -15 -q /usr/sbin/clearshare-scheduler.sh systemd will SIGTERM everything remaining in the service's cgroup anyway... > [Unit] > Description=Siad > After=syslog.target network.target clearsync.service syslog.target is long obsolete. > > [Service] > Type=simple > OOMScoreAdjust=500 > PIDFile=/var/run/siad.pid > EnvironmentFile=/etc/sysconfig/siad > Environment="SIA_DATA_DIR=/var/lib/siad-data" > ExecStartPre=-/usr/bin/killall -15 -q clearshare-scheduler.sh As above. > ExecStartPre=-/usr/bin/rm -f /var/run/siad.pid systemd removes declared PID files automatically. > ExecStart=/usr/bin/siad $EXTRA_ARGS > ExecStop=/usr/bin/siac stop > WorkingDirectory=/var/lib/sia/ > ExecStartPost=/usr/bin/sh -c 'umask 022; /usr/bin/pgrep siad > > /var/run/siad.pid' > > [Install] > WantedBy=multi-user.target > > A "systemctl show clearshare-scheduler" lists the PartOf=siad.service as one > of its properties but, in reverse, "systemctl show siad" does not list the > corresponding ConsistsOf property. systemd loads units lazily, as they are referenced. Thus "systemctl show siad" does not show the deps towards "clearshare-scheduler", then that's because there was no reason to load the latter if you just ask for the former. Usually you would solve that by just adding a "Wants=" line to your unit clearshare-scheduler.service towards siad.service. if you want "losely couple" this, i.e. don't want to modify "siad.ervice" in to point to "clearshare-scheduler.service", then use "WantedBy=siad.service" in "clearshare-scheduler.service"'s [Install] section.q Lennart -- Lennart Poettering, Berlin
Re: [systemd-devel] Starting one service when another one starts
On 09/04/2022 06:17, Andrei Borzenkov wrote: On 08.04.2022 23:35, Nick Howitt wrote: Sorry, for the delay. Big internet outage. On 08/04/2022 15:15, Andrei Borzenkov wrote: On 08.04.2022 14:54, Nick Howitt wrote: Hi, I apologise if this is not the right place for user help. If it is not, please point me to the best place. I am trying to start a service (clearshare-scheduler) when another service (siad) starts. Clearshare-scheduler is an odd service. When you start it it may run for ages (days+) or it may terminate immediately so I have set it up as a oneshot: clearshare-scheduler.service [Unit] Description=Clearshare Scheduler PartOf=siad.service After=siad.service [Service] Type=oneshot Environment="TERM=dumb" ExecStartPre=-/usr/bin/killall -15 -q /usr/sbin/clearshare-scheduler.sh ExecStartPre=-/usr/bin/echo "$(/usr/bin/date) Starting scheduler from systemd" >> /var/log/scheduler.log ExecStart=/usr/sbin/clearshare-scheduler.sh > /dev/null ExecStop=-/usr/bin/killall -15 -q /usr/sbin/clearshare-scheduler.sh [Install] WantedBy=multi-user.target The siad service looks like: siad.service [Unit] Description=Siad After=syslog.target network.target clearsync.service [Service] Type=simple OOMScoreAdjust=500 PIDFile=/var/run/siad.pid EnvironmentFile=/etc/sysconfig/siad Environment="SIA_DATA_DIR=/var/lib/siad-data" ExecStartPre=-/usr/bin/killall -15 -q clearshare-scheduler.sh ExecStartPre=-/usr/bin/rm -f /var/run/siad.pid ExecStart=/usr/bin/siad $EXTRA_ARGS ExecStop=/usr/bin/siac stop WorkingDirectory=/var/lib/sia/ ExecStartPost=/usr/bin/sh -c 'umask 022; /usr/bin/pgrep siad > /var/run/siad.pid' [Install] WantedBy=multi-user.target You do not show actual unit names which makes it rather difficult to follow. Done. See above A "systemctl show clearshare-scheduler" lists the PartOf=siad.service as one of its properties but, in reverse, "systemctl show siad" does not list the corresponding ConsistsOf property. When I start siad, nothing happens to the clearshare-scheduler service. Why do you expect to happen? There is no Wants or Requires in the unit that is /probably/ siad.service so request to start siad.service will not pull in any additional units. Perhaps I have misunderstood, but from the documentation I understood you could PartOf in force (in this case) clearshare-scheduler.service to respond when siad.service was stopped or started. Have I misunderstood the docs? I am hoping to not do any changes to the siad.service. Documentation for PartOf says "limited to stopping and restarting of units". Nothing about "starting". PartOf complements normal startup dependencies, not replaces them. And yes, this is confusing, as are the names of almost any systemd dependency which mean something entirely different from what these names imply in English. You can add WantedBy=siad.service to [Install] section of clearshare-scheduler.service. In general you can always extend Wants by manually creating necessary links. This does not require you to edit unit definition itself. You can also create drop-in (although I am not sure whether they are already supported in your systemd version). I've tried this and can implement what you've described, but as you say, it does not help me when starting the WantedBy service (siad). As an alternative, which does affect the siad.service, is there any way I can run the clearshare-scheduler.sh script from the siad.service? I have tried starting it as a ExecStartPost, but it does not appear to work if the script does not exit immediately. If it runs for a while, then systemd says siad has failed to start. You can increase TimeoutStartSec. I've tried launching it with ExecStartPost=-/usr/sbin/clearshare-scheduler.sh "-" affects command that completed with failure status, in your case command does not complete so this does not have any effect. and ExecStartPost=-/usr/sbin/clearshare-scheduler.sh & and ExecStartPost=-/usr/bin/nohup /usr/sbin/clearshare-scheduler.sh & sytsemd is not shell, what made you think this would work? If you want to use shell syntax, you need to invoke shell /bin/sh -c "/usr/sbin/clearshare-scheduler.sh &" Using bash rather than sh: /bin/bash -c '/usr/sbin/clearshare-scheduler.sh &' This is not producing the results expected. It does not go into the background with the '&' and does not even appear to run. I have simplified the clearshare-scheduler.sh script to just: #!/bin/bash /usr/bin/sleep 10 /usr/bin/echo hello >> /var/log/scheduler.log If I do this the service start fails and times out. At the same time a "ps aux | grep scheduler" never shows the script running and "hello" is not output to /var/log/scheduler.log If I remove the '&', clearshare-scheduler.sh runs and outputs to file, but it is no good if the scheduler script does not terminate for a long time as the service fails when the unit file times out. I think I am stuck here. It does not try to start but it runs when I run it on its own.
Re: [systemd-devel] Starting one service when another one starts
This sort of behavior has been a long-standing desire of many systemd users, and you can find probably thousands of blog/forum/SO posts about it. As Andrei said, the names of the dependencies in systemd units might appear to be the behavior you want, but they aren't. If you have a situation where service B must always be running when service A is running, and never when service A is not running, a combination of "Upholds" and "PropagatesStopTo" will do that, although you'll need very recent systemd to have these available. On Sat, Apr 9, 2022 at 1:17 AM Andrei Borzenkov wrote: > > On 08.04.2022 23:35, Nick Howitt wrote: > > Sorry, for the delay. Big internet outage. > > > > On 08/04/2022 15:15, Andrei Borzenkov wrote: > >> > >> On 08.04.2022 14:54, Nick Howitt wrote: > >>> Hi, > >>> I apologise if this is not the right place for user help. If it is not, > >>> please point me to the best place. > >>> > >>> I am trying to start a service (clearshare-scheduler) when another > >>> service (siad) starts. Clearshare-scheduler is an odd service. When you > >>> start it it may run for ages (days+) or it may terminate immediately so > >>> I have set it up as a oneshot: > >>> > > clearshare-scheduler.service > >>> [Unit] > >>> Description=Clearshare Scheduler > >>> PartOf=siad.service > >>> After=siad.service > >>> > >>> [Service] > >>> Type=oneshot > >>> Environment="TERM=dumb" > >>> ExecStartPre=-/usr/bin/killall -15 -q /usr/sbin/clearshare-scheduler.sh > >>> ExecStartPre=-/usr/bin/echo "$(/usr/bin/date) Starting scheduler from > >>> systemd" >> /var/log/scheduler.log > >>> ExecStart=/usr/sbin/clearshare-scheduler.sh > /dev/null > >>> ExecStop=-/usr/bin/killall -15 -q /usr/sbin/clearshare-scheduler.sh > >>> > >>> [Install] > >>> WantedBy=multi-user.target > >>> > >>> The siad service looks like: > >>> > > siad.service > >>> [Unit] > >>> Description=Siad > >>> After=syslog.target network.target clearsync.service > >>> > >>> [Service] > >>> Type=simple > >>> OOMScoreAdjust=500 > >>> PIDFile=/var/run/siad.pid > >>> EnvironmentFile=/etc/sysconfig/siad > >>> Environment="SIA_DATA_DIR=/var/lib/siad-data" > >>> ExecStartPre=-/usr/bin/killall -15 -q clearshare-scheduler.sh > >>> ExecStartPre=-/usr/bin/rm -f /var/run/siad.pid > >>> ExecStart=/usr/bin/siad $EXTRA_ARGS > >>> ExecStop=/usr/bin/siac stop > >>> WorkingDirectory=/var/lib/sia/ > >>> ExecStartPost=/usr/bin/sh -c 'umask 022; /usr/bin/pgrep siad > > >>> /var/run/siad.pid' > >>> > >>> [Install] > >>> WantedBy=multi-user.target > >>> > >> > >> You do not show actual unit names which makes it rather difficult to > >> follow. > > Done. See above > >> > >>> A "systemctl show clearshare-scheduler" lists the PartOf=siad.service as > >>> one of its properties but, in reverse, "systemctl show siad" does not > >>> list the corresponding ConsistsOf property. > >>> > >>> When I start siad, nothing happens to the clearshare-scheduler service. > >> > >> Why do you expect to happen? There is no Wants or Requires in the unit > >> that is /probably/ siad.service so request to start siad.service will > >> not pull in any additional units. > > Perhaps I have misunderstood, but from the documentation I understood > > you could PartOf in force (in this case) clearshare-scheduler.service to > > respond when siad.service was stopped or started. Have I misunderstood > > the docs? I am hoping to not do any changes to the siad.service. > > > > Documentation for PartOf says "limited to stopping and restarting of > units". Nothing about "starting". PartOf complements normal startup > dependencies, not replaces them. And yes, this is confusing, as are the > names of almost any systemd dependency which mean something entirely > different from what these names imply in English. > > You can add WantedBy=siad.service to [Install] section of > clearshare-scheduler.service. In general you can always extend Wants by > manually creating necessary links. This does not require you to edit > unit definition itself. You can also create drop-in (although I am not > sure whether they are already supported in your systemd version). > > > As an alternative, which does affect the siad.service, is there any way > > I can run the clearshare-scheduler.sh script from the siad.service? I > > have tried starting it as a ExecStartPost, but it does not appear to > > work if the script does not exit immediately. If it runs for a while, > > then systemd says siad has failed to start. > > You can increase TimeoutStartSec. > > > I've tried launching it with > > ExecStartPost=-/usr/sbin/clearshare-scheduler.sh > > "-" affects command that completed with failure status, in your case > command does not complete so this does not have any effect. > > > and > > ExecStartPost=-/usr/sbin/clearshare-scheduler.sh & > > and > > ExecStartPost=-/usr/bin/nohup /usr/sbin/clearshare-scheduler.sh & > > > > sytsemd is not shell, what made you think this would work? If you want > to use shell syntax, you need to invoke shell > > /bin/sh
Re: [systemd-devel] Starting one service when another one starts
On 08.04.2022 23:35, Nick Howitt wrote: > Sorry, for the delay. Big internet outage. > > On 08/04/2022 15:15, Andrei Borzenkov wrote: >> >> On 08.04.2022 14:54, Nick Howitt wrote: >>> Hi, >>> I apologise if this is not the right place for user help. If it is not, >>> please point me to the best place. >>> >>> I am trying to start a service (clearshare-scheduler) when another >>> service (siad) starts. Clearshare-scheduler is an odd service. When you >>> start it it may run for ages (days+) or it may terminate immediately so >>> I have set it up as a oneshot: >>> > clearshare-scheduler.service >>> [Unit] >>> Description=Clearshare Scheduler >>> PartOf=siad.service >>> After=siad.service >>> >>> [Service] >>> Type=oneshot >>> Environment="TERM=dumb" >>> ExecStartPre=-/usr/bin/killall -15 -q /usr/sbin/clearshare-scheduler.sh >>> ExecStartPre=-/usr/bin/echo "$(/usr/bin/date) Starting scheduler from >>> systemd" >> /var/log/scheduler.log >>> ExecStart=/usr/sbin/clearshare-scheduler.sh > /dev/null >>> ExecStop=-/usr/bin/killall -15 -q /usr/sbin/clearshare-scheduler.sh >>> >>> [Install] >>> WantedBy=multi-user.target >>> >>> The siad service looks like: >>> > siad.service >>> [Unit] >>> Description=Siad >>> After=syslog.target network.target clearsync.service >>> >>> [Service] >>> Type=simple >>> OOMScoreAdjust=500 >>> PIDFile=/var/run/siad.pid >>> EnvironmentFile=/etc/sysconfig/siad >>> Environment="SIA_DATA_DIR=/var/lib/siad-data" >>> ExecStartPre=-/usr/bin/killall -15 -q clearshare-scheduler.sh >>> ExecStartPre=-/usr/bin/rm -f /var/run/siad.pid >>> ExecStart=/usr/bin/siad $EXTRA_ARGS >>> ExecStop=/usr/bin/siac stop >>> WorkingDirectory=/var/lib/sia/ >>> ExecStartPost=/usr/bin/sh -c 'umask 022; /usr/bin/pgrep siad > >>> /var/run/siad.pid' >>> >>> [Install] >>> WantedBy=multi-user.target >>> >> >> You do not show actual unit names which makes it rather difficult to follow. > Done. See above >> >>> A "systemctl show clearshare-scheduler" lists the PartOf=siad.service as >>> one of its properties but, in reverse, "systemctl show siad" does not >>> list the corresponding ConsistsOf property. >>> >>> When I start siad, nothing happens to the clearshare-scheduler service. >> >> Why do you expect to happen? There is no Wants or Requires in the unit >> that is /probably/ siad.service so request to start siad.service will >> not pull in any additional units. > Perhaps I have misunderstood, but from the documentation I understood > you could PartOf in force (in this case) clearshare-scheduler.service to > respond when siad.service was stopped or started. Have I misunderstood > the docs? I am hoping to not do any changes to the siad.service. > Documentation for PartOf says "limited to stopping and restarting of units". Nothing about "starting". PartOf complements normal startup dependencies, not replaces them. And yes, this is confusing, as are the names of almost any systemd dependency which mean something entirely different from what these names imply in English. You can add WantedBy=siad.service to [Install] section of clearshare-scheduler.service. In general you can always extend Wants by manually creating necessary links. This does not require you to edit unit definition itself. You can also create drop-in (although I am not sure whether they are already supported in your systemd version). > As an alternative, which does affect the siad.service, is there any way > I can run the clearshare-scheduler.sh script from the siad.service? I > have tried starting it as a ExecStartPost, but it does not appear to > work if the script does not exit immediately. If it runs for a while, > then systemd says siad has failed to start. You can increase TimeoutStartSec. > I've tried launching it with > ExecStartPost=-/usr/sbin/clearshare-scheduler.sh "-" affects command that completed with failure status, in your case command does not complete so this does not have any effect. > and > ExecStartPost=-/usr/sbin/clearshare-scheduler.sh & > and > ExecStartPost=-/usr/bin/nohup /usr/sbin/clearshare-scheduler.sh & > sytsemd is not shell, what made you think this would work? If you want to use shell syntax, you need to invoke shell /bin/sh -c "/usr/sbin/clearshare-scheduler.sh &" >> >>> It does not try to start but it runs when I run it on its own. Have I >>> misunderstood something? >>> >>> My version of systems is systemd-219-78.el7_9.5. >>> >>> Thanks, >>> >>> Nick >>
Re: [systemd-devel] Starting one service when another one starts
Sorry, for the delay. Big internet outage. On 08/04/2022 15:15, Andrei Borzenkov wrote: On 08.04.2022 14:54, Nick Howitt wrote: Hi, I apologise if this is not the right place for user help. If it is not, please point me to the best place. I am trying to start a service (clearshare-scheduler) when another service (siad) starts. Clearshare-scheduler is an odd service. When you start it it may run for ages (days+) or it may terminate immediately so I have set it up as a oneshot: clearshare-scheduler.service [Unit] Description=Clearshare Scheduler PartOf=siad.service After=siad.service [Service] Type=oneshot Environment="TERM=dumb" ExecStartPre=-/usr/bin/killall -15 -q /usr/sbin/clearshare-scheduler.sh ExecStartPre=-/usr/bin/echo "$(/usr/bin/date) Starting scheduler from systemd" >> /var/log/scheduler.log ExecStart=/usr/sbin/clearshare-scheduler.sh > /dev/null ExecStop=-/usr/bin/killall -15 -q /usr/sbin/clearshare-scheduler.sh [Install] WantedBy=multi-user.target The siad service looks like: siad.service [Unit] Description=Siad After=syslog.target network.target clearsync.service [Service] Type=simple OOMScoreAdjust=500 PIDFile=/var/run/siad.pid EnvironmentFile=/etc/sysconfig/siad Environment="SIA_DATA_DIR=/var/lib/siad-data" ExecStartPre=-/usr/bin/killall -15 -q clearshare-scheduler.sh ExecStartPre=-/usr/bin/rm -f /var/run/siad.pid ExecStart=/usr/bin/siad $EXTRA_ARGS ExecStop=/usr/bin/siac stop WorkingDirectory=/var/lib/sia/ ExecStartPost=/usr/bin/sh -c 'umask 022; /usr/bin/pgrep siad > /var/run/siad.pid' [Install] WantedBy=multi-user.target You do not show actual unit names which makes it rather difficult to follow. Done. See above A "systemctl show clearshare-scheduler" lists the PartOf=siad.service as one of its properties but, in reverse, "systemctl show siad" does not list the corresponding ConsistsOf property. When I start siad, nothing happens to the clearshare-scheduler service. Why do you expect to happen? There is no Wants or Requires in the unit that is /probably/ siad.service so request to start siad.service will not pull in any additional units. Perhaps I have misunderstood, but from the documentation I understood you could PartOf in force (in this case) clearshare-scheduler.service to respond when siad.service was stopped or started. Have I misunderstood the docs? I am hoping to not do any changes to the siad.service. As an alternative, which does affect the siad.service, is there any way I can run the clearshare-scheduler.sh script from the siad.service? I have tried starting it as a ExecStartPost, but it does not appear to work if the script does not exit immediately. If it runs for a while, then systemd says siad has failed to start. I've tried launching it with ExecStartPost=-/usr/sbin/clearshare-scheduler.sh and ExecStartPost=-/usr/sbin/clearshare-scheduler.sh & and ExecStartPost=-/usr/bin/nohup /usr/sbin/clearshare-scheduler.sh & It does not try to start but it runs when I run it on its own. Have I misunderstood something? My version of systems is systemd-219-78.el7_9.5. Thanks, Nick
Re: [systemd-devel] Starting one service when another one starts
On 08.04.2022 14:54, Nick Howitt wrote: > Hi, > I apologise if this is not the right place for user help. If it is not, > please point me to the best place. > > I am trying to start a service (clearshare-scheduler) when another > service (siad) starts. Clearshare-scheduler is an odd service. When you > start it it may run for ages (days+) or it may terminate immediately so > I have set it up as a oneshot: > > [Unit] > Description=Clearshare Scheduler > PartOf=siad.service > After=siad.service > > [Service] > Type=oneshot > Environment="TERM=dumb" > ExecStartPre=-/usr/bin/killall -15 -q /usr/sbin/clearshare-scheduler.sh > ExecStartPre=-/usr/bin/echo "$(/usr/bin/date) Starting scheduler from > systemd" >> /var/log/scheduler.log > ExecStart=/usr/sbin/clearshare-scheduler.sh > /dev/null > ExecStop=-/usr/bin/killall -15 -q /usr/sbin/clearshare-scheduler.sh > > [Install] > WantedBy=multi-user.target > > The siad service looks like: > > [Unit] > Description=Siad > After=syslog.target network.target clearsync.service > > [Service] > Type=simple > OOMScoreAdjust=500 > PIDFile=/var/run/siad.pid > EnvironmentFile=/etc/sysconfig/siad > Environment="SIA_DATA_DIR=/var/lib/siad-data" > ExecStartPre=-/usr/bin/killall -15 -q clearshare-scheduler.sh > ExecStartPre=-/usr/bin/rm -f /var/run/siad.pid > ExecStart=/usr/bin/siad $EXTRA_ARGS > ExecStop=/usr/bin/siac stop > WorkingDirectory=/var/lib/sia/ > ExecStartPost=/usr/bin/sh -c 'umask 022; /usr/bin/pgrep siad > > /var/run/siad.pid' > > [Install] > WantedBy=multi-user.target > You do not show actual unit names which makes it rather difficult to follow. > A "systemctl show clearshare-scheduler" lists the PartOf=siad.service as > one of its properties but, in reverse, "systemctl show siad" does not > list the corresponding ConsistsOf property. > > When I start siad, nothing happens to the clearshare-scheduler service. Why do you expect to happen? There is no Wants or Requires in the unit that is /probably/ siad.service so request to start siad.service will not pull in any additional units. > It does not try to start but it runs when I run it on its own. Have I > misunderstood something? > > My version of systems is systemd-219-78.el7_9.5. > > Thanks, > > Nick