shut down system when multiple systemd units stop

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite












I'm trying to shut down the computer when a specific list of systemd units stop.



The background is that I have a compute farm in Google cloud & AWS EC2, and each instance launches a couple services which perform a bunch of work and then exit when complete. Since these are cloud instances and cost money, the instance should shut down once it is no longer doing anything.



Most suggestions I've seen around this involve putting an ExecStopPost=/sbin/init 0 (or similar) in the systemd unit file. However I cannot shut the instance down until all the units have completed.
It's easy enough for me to create a target unit which wraps the individual worker units, and then put a StopWhenUnneeded=true in it so that the target stops once all the workers are stopped. But I don't see a way to use this to trigger the system to shut down (for example by invoking systemd-halt.service).



So the question is: how can I get the system to shut down once a list of units, or my target unit, stops.







share|improve this question


























    up vote
    4
    down vote

    favorite












    I'm trying to shut down the computer when a specific list of systemd units stop.



    The background is that I have a compute farm in Google cloud & AWS EC2, and each instance launches a couple services which perform a bunch of work and then exit when complete. Since these are cloud instances and cost money, the instance should shut down once it is no longer doing anything.



    Most suggestions I've seen around this involve putting an ExecStopPost=/sbin/init 0 (or similar) in the systemd unit file. However I cannot shut the instance down until all the units have completed.
    It's easy enough for me to create a target unit which wraps the individual worker units, and then put a StopWhenUnneeded=true in it so that the target stops once all the workers are stopped. But I don't see a way to use this to trigger the system to shut down (for example by invoking systemd-halt.service).



    So the question is: how can I get the system to shut down once a list of units, or my target unit, stops.







    share|improve this question
























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      I'm trying to shut down the computer when a specific list of systemd units stop.



      The background is that I have a compute farm in Google cloud & AWS EC2, and each instance launches a couple services which perform a bunch of work and then exit when complete. Since these are cloud instances and cost money, the instance should shut down once it is no longer doing anything.



      Most suggestions I've seen around this involve putting an ExecStopPost=/sbin/init 0 (or similar) in the systemd unit file. However I cannot shut the instance down until all the units have completed.
      It's easy enough for me to create a target unit which wraps the individual worker units, and then put a StopWhenUnneeded=true in it so that the target stops once all the workers are stopped. But I don't see a way to use this to trigger the system to shut down (for example by invoking systemd-halt.service).



      So the question is: how can I get the system to shut down once a list of units, or my target unit, stops.







      share|improve this question














      I'm trying to shut down the computer when a specific list of systemd units stop.



      The background is that I have a compute farm in Google cloud & AWS EC2, and each instance launches a couple services which perform a bunch of work and then exit when complete. Since these are cloud instances and cost money, the instance should shut down once it is no longer doing anything.



      Most suggestions I've seen around this involve putting an ExecStopPost=/sbin/init 0 (or similar) in the systemd unit file. However I cannot shut the instance down until all the units have completed.
      It's easy enough for me to create a target unit which wraps the individual worker units, and then put a StopWhenUnneeded=true in it so that the target stops once all the workers are stopped. But I don't see a way to use this to trigger the system to shut down (for example by invoking systemd-halt.service).



      So the question is: how can I get the system to shut down once a list of units, or my target unit, stops.









      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 30 '17 at 4:30

























      asked Oct 30 '17 at 1:29









      Patrick

      47.9k11125176




      47.9k11125176




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          After some experimentation, one working, not entirely ugly, solution I've come up with is to use the idea of the wrapper target unit, but use a service instead which shuts the system down on exit.



          For example, the worker service:



          # worker@.service
          [Unit]
          BindsTo=master.service
          After=master.service

          [Service]
          ExecStart=/bin/myworker

          [Install]
          WantedBy=master.service


          And then for the wrapper:



          # master.service
          [Unit]
          StopWhenUnneeded=true

          [Service]
          Type=oneshot
          RemainAfterExit=true
          ExecStart=/bin/true
          ExecStop=/sbin/init 0


          I can then do systemctl start master.service to start all the workers (after systemctl enableing them), and when they all exit, master.service will stop and execute init 0.
          systemctl stop master.service will also signal the worker units to stop, but will wait for them before executing the init 0.



          The ugliness is having to create a fake service which executes /bin/true. It would be nicer if there were an existing systemd unit that when stopped would result in the system shutting down, but this is at least functional.






          share|improve this answer




















          • It's not that ugly though.
            – EightBitTony
            Oct 30 '17 at 9:45










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f401328%2fshut-down-system-when-multiple-systemd-units-stop%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          After some experimentation, one working, not entirely ugly, solution I've come up with is to use the idea of the wrapper target unit, but use a service instead which shuts the system down on exit.



          For example, the worker service:



          # worker@.service
          [Unit]
          BindsTo=master.service
          After=master.service

          [Service]
          ExecStart=/bin/myworker

          [Install]
          WantedBy=master.service


          And then for the wrapper:



          # master.service
          [Unit]
          StopWhenUnneeded=true

          [Service]
          Type=oneshot
          RemainAfterExit=true
          ExecStart=/bin/true
          ExecStop=/sbin/init 0


          I can then do systemctl start master.service to start all the workers (after systemctl enableing them), and when they all exit, master.service will stop and execute init 0.
          systemctl stop master.service will also signal the worker units to stop, but will wait for them before executing the init 0.



          The ugliness is having to create a fake service which executes /bin/true. It would be nicer if there were an existing systemd unit that when stopped would result in the system shutting down, but this is at least functional.






          share|improve this answer




















          • It's not that ugly though.
            – EightBitTony
            Oct 30 '17 at 9:45














          up vote
          2
          down vote



          accepted










          After some experimentation, one working, not entirely ugly, solution I've come up with is to use the idea of the wrapper target unit, but use a service instead which shuts the system down on exit.



          For example, the worker service:



          # worker@.service
          [Unit]
          BindsTo=master.service
          After=master.service

          [Service]
          ExecStart=/bin/myworker

          [Install]
          WantedBy=master.service


          And then for the wrapper:



          # master.service
          [Unit]
          StopWhenUnneeded=true

          [Service]
          Type=oneshot
          RemainAfterExit=true
          ExecStart=/bin/true
          ExecStop=/sbin/init 0


          I can then do systemctl start master.service to start all the workers (after systemctl enableing them), and when they all exit, master.service will stop and execute init 0.
          systemctl stop master.service will also signal the worker units to stop, but will wait for them before executing the init 0.



          The ugliness is having to create a fake service which executes /bin/true. It would be nicer if there were an existing systemd unit that when stopped would result in the system shutting down, but this is at least functional.






          share|improve this answer




















          • It's not that ugly though.
            – EightBitTony
            Oct 30 '17 at 9:45












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          After some experimentation, one working, not entirely ugly, solution I've come up with is to use the idea of the wrapper target unit, but use a service instead which shuts the system down on exit.



          For example, the worker service:



          # worker@.service
          [Unit]
          BindsTo=master.service
          After=master.service

          [Service]
          ExecStart=/bin/myworker

          [Install]
          WantedBy=master.service


          And then for the wrapper:



          # master.service
          [Unit]
          StopWhenUnneeded=true

          [Service]
          Type=oneshot
          RemainAfterExit=true
          ExecStart=/bin/true
          ExecStop=/sbin/init 0


          I can then do systemctl start master.service to start all the workers (after systemctl enableing them), and when they all exit, master.service will stop and execute init 0.
          systemctl stop master.service will also signal the worker units to stop, but will wait for them before executing the init 0.



          The ugliness is having to create a fake service which executes /bin/true. It would be nicer if there were an existing systemd unit that when stopped would result in the system shutting down, but this is at least functional.






          share|improve this answer












          After some experimentation, one working, not entirely ugly, solution I've come up with is to use the idea of the wrapper target unit, but use a service instead which shuts the system down on exit.



          For example, the worker service:



          # worker@.service
          [Unit]
          BindsTo=master.service
          After=master.service

          [Service]
          ExecStart=/bin/myworker

          [Install]
          WantedBy=master.service


          And then for the wrapper:



          # master.service
          [Unit]
          StopWhenUnneeded=true

          [Service]
          Type=oneshot
          RemainAfterExit=true
          ExecStart=/bin/true
          ExecStop=/sbin/init 0


          I can then do systemctl start master.service to start all the workers (after systemctl enableing them), and when they all exit, master.service will stop and execute init 0.
          systemctl stop master.service will also signal the worker units to stop, but will wait for them before executing the init 0.



          The ugliness is having to create a fake service which executes /bin/true. It would be nicer if there were an existing systemd unit that when stopped would result in the system shutting down, but this is at least functional.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 30 '17 at 4:30









          Patrick

          47.9k11125176




          47.9k11125176











          • It's not that ugly though.
            – EightBitTony
            Oct 30 '17 at 9:45
















          • It's not that ugly though.
            – EightBitTony
            Oct 30 '17 at 9:45















          It's not that ugly though.
          – EightBitTony
          Oct 30 '17 at 9:45




          It's not that ugly though.
          – EightBitTony
          Oct 30 '17 at 9:45

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f401328%2fshut-down-system-when-multiple-systemd-units-stop%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay