Systemd user unit that depends on system unit (sleep.target)












13















I was reading doc and it is still unclear for me, whether the following is possible to accomplish:



service defined in ~/.config/systemd/user/task.service that depends on system sleep.target (~/.config/systemd/user/sleep.target.wants/task.service).



Now I expect task.service to start when I run $ systemctl suspend, however task.service is not started.



I'm running debian, with systemd version 208, systemd --user configured more or less as described on the ArchWiki.



I wonder whether my scenario could be implemented with systemd at all, or are --system and --user completely isolated by design so that --user unit may not be a dependency of a --system unit.



In case it is possible, what might be the problem in my case?










share|improve this question

























  • Similar issue

    – l0b0
    Aug 12 '14 at 21:58
















13















I was reading doc and it is still unclear for me, whether the following is possible to accomplish:



service defined in ~/.config/systemd/user/task.service that depends on system sleep.target (~/.config/systemd/user/sleep.target.wants/task.service).



Now I expect task.service to start when I run $ systemctl suspend, however task.service is not started.



I'm running debian, with systemd version 208, systemd --user configured more or less as described on the ArchWiki.



I wonder whether my scenario could be implemented with systemd at all, or are --system and --user completely isolated by design so that --user unit may not be a dependency of a --system unit.



In case it is possible, what might be the problem in my case?










share|improve this question

























  • Similar issue

    – l0b0
    Aug 12 '14 at 21:58














13












13








13


3






I was reading doc and it is still unclear for me, whether the following is possible to accomplish:



service defined in ~/.config/systemd/user/task.service that depends on system sleep.target (~/.config/systemd/user/sleep.target.wants/task.service).



Now I expect task.service to start when I run $ systemctl suspend, however task.service is not started.



I'm running debian, with systemd version 208, systemd --user configured more or less as described on the ArchWiki.



I wonder whether my scenario could be implemented with systemd at all, or are --system and --user completely isolated by design so that --user unit may not be a dependency of a --system unit.



In case it is possible, what might be the problem in my case?










share|improve this question
















I was reading doc and it is still unclear for me, whether the following is possible to accomplish:



service defined in ~/.config/systemd/user/task.service that depends on system sleep.target (~/.config/systemd/user/sleep.target.wants/task.service).



Now I expect task.service to start when I run $ systemctl suspend, however task.service is not started.



I'm running debian, with systemd version 208, systemd --user configured more or less as described on the ArchWiki.



I wonder whether my scenario could be implemented with systemd at all, or are --system and --user completely isolated by design so that --user unit may not be a dependency of a --system unit.



In case it is possible, what might be the problem in my case?







systemd






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 1 '14 at 18:29









HalosGhost

3,72592236




3,72592236










asked Aug 1 '14 at 18:22









Alexander KitaevAlexander Kitaev

1664




1664













  • Similar issue

    – l0b0
    Aug 12 '14 at 21:58



















  • Similar issue

    – l0b0
    Aug 12 '14 at 21:58

















Similar issue

– l0b0
Aug 12 '14 at 21:58





Similar issue

– l0b0
Aug 12 '14 at 21:58










3 Answers
3






active

oldest

votes


















5














systemd user session services run in a completely separate instance of systemd, and doesn’t have any way to depend on system services directly.



There are other ways to accomplish what you want though. The cleanest would probably be to make whatever you want to run when the system is going to sleep hook into logind’s inhibitors and then run it as a background daemon.



A more general solution would be to have a daemon hooks into the logind inhibitors, (see systemd-lock-handler and xss-lock,) and then when the system is going to sleep it will activate a user session target that you can order your services under.






share|improve this answer































    5














    From systemd/User - Archwiki




    systemd --user runs as a separate process from the systemd --system process. User units can not reference or depend on system units.







    share|improve this answer



















    • 2





      This sucks. That's too bad.

      – Rolf
      Mar 29 '18 at 12:49



















    0














    Adding to the response from @kyrias, here's a way to create your own user level sleep.target:



    ~/.local/share/systemd/user/sleep.target



    [Unit]
    Description=User level sleep target
    StopWhenUnneeded=yes


    ~/.local/bin/watch_sleep



    #!/bin/bash

    dbus-monitor --system "type='signal',interface='org.freedesktop.login1.Manager',member=PrepareForSleep" | while read x; do
    case "$x" in
    *"boolean false"*) systemctl --user --no-block stop sleep.target;;
    *"boolean true"*) systemctl --user --no-block start sleep.target;;
    esac
    done


    ~/.local/share/systemd/user/watch_sleep.service



    [Unit]
    Description=watch for sleep signal to start sleep.target

    [Service]
    ExecStart=%h/.local/bin/watch_sleep
    Restart=on-failure

    [Install]
    WantedBy=default.target


    See my blog post https://medium.com/@aiguofer/systemd-sleep-target-for-user-level-10eb003b3bfd





    share








    New contributor




    aiguofer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.




















      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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      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%2f147904%2fsystemd-user-unit-that-depends-on-system-unit-sleep-target%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      5














      systemd user session services run in a completely separate instance of systemd, and doesn’t have any way to depend on system services directly.



      There are other ways to accomplish what you want though. The cleanest would probably be to make whatever you want to run when the system is going to sleep hook into logind’s inhibitors and then run it as a background daemon.



      A more general solution would be to have a daemon hooks into the logind inhibitors, (see systemd-lock-handler and xss-lock,) and then when the system is going to sleep it will activate a user session target that you can order your services under.






      share|improve this answer




























        5














        systemd user session services run in a completely separate instance of systemd, and doesn’t have any way to depend on system services directly.



        There are other ways to accomplish what you want though. The cleanest would probably be to make whatever you want to run when the system is going to sleep hook into logind’s inhibitors and then run it as a background daemon.



        A more general solution would be to have a daemon hooks into the logind inhibitors, (see systemd-lock-handler and xss-lock,) and then when the system is going to sleep it will activate a user session target that you can order your services under.






        share|improve this answer


























          5












          5








          5







          systemd user session services run in a completely separate instance of systemd, and doesn’t have any way to depend on system services directly.



          There are other ways to accomplish what you want though. The cleanest would probably be to make whatever you want to run when the system is going to sleep hook into logind’s inhibitors and then run it as a background daemon.



          A more general solution would be to have a daemon hooks into the logind inhibitors, (see systemd-lock-handler and xss-lock,) and then when the system is going to sleep it will activate a user session target that you can order your services under.






          share|improve this answer













          systemd user session services run in a completely separate instance of systemd, and doesn’t have any way to depend on system services directly.



          There are other ways to accomplish what you want though. The cleanest would probably be to make whatever you want to run when the system is going to sleep hook into logind’s inhibitors and then run it as a background daemon.



          A more general solution would be to have a daemon hooks into the logind inhibitors, (see systemd-lock-handler and xss-lock,) and then when the system is going to sleep it will activate a user session target that you can order your services under.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 20 '15 at 15:25









          kyriaskyrias

          3,05511629




          3,05511629

























              5














              From systemd/User - Archwiki




              systemd --user runs as a separate process from the systemd --system process. User units can not reference or depend on system units.







              share|improve this answer



















              • 2





                This sucks. That's too bad.

                – Rolf
                Mar 29 '18 at 12:49
















              5














              From systemd/User - Archwiki




              systemd --user runs as a separate process from the systemd --system process. User units can not reference or depend on system units.







              share|improve this answer



















              • 2





                This sucks. That's too bad.

                – Rolf
                Mar 29 '18 at 12:49














              5












              5








              5







              From systemd/User - Archwiki




              systemd --user runs as a separate process from the systemd --system process. User units can not reference or depend on system units.







              share|improve this answer













              From systemd/User - Archwiki




              systemd --user runs as a separate process from the systemd --system process. User units can not reference or depend on system units.








              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Apr 23 '16 at 12:03









              sshowsshow

              15317




              15317








              • 2





                This sucks. That's too bad.

                – Rolf
                Mar 29 '18 at 12:49














              • 2





                This sucks. That's too bad.

                – Rolf
                Mar 29 '18 at 12:49








              2




              2





              This sucks. That's too bad.

              – Rolf
              Mar 29 '18 at 12:49





              This sucks. That's too bad.

              – Rolf
              Mar 29 '18 at 12:49











              0














              Adding to the response from @kyrias, here's a way to create your own user level sleep.target:



              ~/.local/share/systemd/user/sleep.target



              [Unit]
              Description=User level sleep target
              StopWhenUnneeded=yes


              ~/.local/bin/watch_sleep



              #!/bin/bash

              dbus-monitor --system "type='signal',interface='org.freedesktop.login1.Manager',member=PrepareForSleep" | while read x; do
              case "$x" in
              *"boolean false"*) systemctl --user --no-block stop sleep.target;;
              *"boolean true"*) systemctl --user --no-block start sleep.target;;
              esac
              done


              ~/.local/share/systemd/user/watch_sleep.service



              [Unit]
              Description=watch for sleep signal to start sleep.target

              [Service]
              ExecStart=%h/.local/bin/watch_sleep
              Restart=on-failure

              [Install]
              WantedBy=default.target


              See my blog post https://medium.com/@aiguofer/systemd-sleep-target-for-user-level-10eb003b3bfd





              share








              New contributor




              aiguofer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.

























                0














                Adding to the response from @kyrias, here's a way to create your own user level sleep.target:



                ~/.local/share/systemd/user/sleep.target



                [Unit]
                Description=User level sleep target
                StopWhenUnneeded=yes


                ~/.local/bin/watch_sleep



                #!/bin/bash

                dbus-monitor --system "type='signal',interface='org.freedesktop.login1.Manager',member=PrepareForSleep" | while read x; do
                case "$x" in
                *"boolean false"*) systemctl --user --no-block stop sleep.target;;
                *"boolean true"*) systemctl --user --no-block start sleep.target;;
                esac
                done


                ~/.local/share/systemd/user/watch_sleep.service



                [Unit]
                Description=watch for sleep signal to start sleep.target

                [Service]
                ExecStart=%h/.local/bin/watch_sleep
                Restart=on-failure

                [Install]
                WantedBy=default.target


                See my blog post https://medium.com/@aiguofer/systemd-sleep-target-for-user-level-10eb003b3bfd





                share








                New contributor




                aiguofer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.























                  0












                  0








                  0







                  Adding to the response from @kyrias, here's a way to create your own user level sleep.target:



                  ~/.local/share/systemd/user/sleep.target



                  [Unit]
                  Description=User level sleep target
                  StopWhenUnneeded=yes


                  ~/.local/bin/watch_sleep



                  #!/bin/bash

                  dbus-monitor --system "type='signal',interface='org.freedesktop.login1.Manager',member=PrepareForSleep" | while read x; do
                  case "$x" in
                  *"boolean false"*) systemctl --user --no-block stop sleep.target;;
                  *"boolean true"*) systemctl --user --no-block start sleep.target;;
                  esac
                  done


                  ~/.local/share/systemd/user/watch_sleep.service



                  [Unit]
                  Description=watch for sleep signal to start sleep.target

                  [Service]
                  ExecStart=%h/.local/bin/watch_sleep
                  Restart=on-failure

                  [Install]
                  WantedBy=default.target


                  See my blog post https://medium.com/@aiguofer/systemd-sleep-target-for-user-level-10eb003b3bfd





                  share








                  New contributor




                  aiguofer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.










                  Adding to the response from @kyrias, here's a way to create your own user level sleep.target:



                  ~/.local/share/systemd/user/sleep.target



                  [Unit]
                  Description=User level sleep target
                  StopWhenUnneeded=yes


                  ~/.local/bin/watch_sleep



                  #!/bin/bash

                  dbus-monitor --system "type='signal',interface='org.freedesktop.login1.Manager',member=PrepareForSleep" | while read x; do
                  case "$x" in
                  *"boolean false"*) systemctl --user --no-block stop sleep.target;;
                  *"boolean true"*) systemctl --user --no-block start sleep.target;;
                  esac
                  done


                  ~/.local/share/systemd/user/watch_sleep.service



                  [Unit]
                  Description=watch for sleep signal to start sleep.target

                  [Service]
                  ExecStart=%h/.local/bin/watch_sleep
                  Restart=on-failure

                  [Install]
                  WantedBy=default.target


                  See my blog post https://medium.com/@aiguofer/systemd-sleep-target-for-user-level-10eb003b3bfd






                  share








                  New contributor




                  aiguofer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.








                  share


                  share






                  New contributor




                  aiguofer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 2 mins ago









                  aiguoferaiguofer

                  1012




                  1012




                  New contributor




                  aiguofer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  aiguofer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  aiguofer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f147904%2fsystemd-user-unit-that-depends-on-system-unit-sleep-target%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      宮崎県

                      濃尾地震

                      シテ島