Can systemd handle double-fork daemons?

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











up vote
0
down vote

favorite












Can systemd handle double-fork daemons ?



I previously used init.d/ script to manage daemons I wrote which have worked flawless for years. Now I find myself needing to provide a systemd/ script for and I cannot get that to work with the way I do daemon setup code.



When I setup a daemon I fork twice to loose controlling terminal and avoid being session leader.



The problem with this is that I have confirmed that when you do that systemd/ looses track and kills the daemon. I have also confirmed that skipping the second fork makes it work again with systemd/.



My basic (slightly simplified for the purpose of this discussion) systemd/ script is



[Unit]
Description=GM7 Service Daemon

[Service]
Type=forking
ExecStart=/usr/bin/g7ctrl

[Install]
WantedBy=multi-user.target


So I guess my question is : Do I need to change the way I have done daemon setup code in C/C++ for the last decade or is there is options to get systemd/ to track a double-fork ?



I haven't quite read-up on the inner workings if systemd itself spawns processes for each daemon it starts in which case my double-fork would indeed be obsolete



I admit that the way I have done it (double fork) is partially for historic reasons but at the time I did some quite intensive read-up on best-practice and this is what I then came up with (a decade or so ago)









share

























    up vote
    0
    down vote

    favorite












    Can systemd handle double-fork daemons ?



    I previously used init.d/ script to manage daemons I wrote which have worked flawless for years. Now I find myself needing to provide a systemd/ script for and I cannot get that to work with the way I do daemon setup code.



    When I setup a daemon I fork twice to loose controlling terminal and avoid being session leader.



    The problem with this is that I have confirmed that when you do that systemd/ looses track and kills the daemon. I have also confirmed that skipping the second fork makes it work again with systemd/.



    My basic (slightly simplified for the purpose of this discussion) systemd/ script is



    [Unit]
    Description=GM7 Service Daemon

    [Service]
    Type=forking
    ExecStart=/usr/bin/g7ctrl

    [Install]
    WantedBy=multi-user.target


    So I guess my question is : Do I need to change the way I have done daemon setup code in C/C++ for the last decade or is there is options to get systemd/ to track a double-fork ?



    I haven't quite read-up on the inner workings if systemd itself spawns processes for each daemon it starts in which case my double-fork would indeed be obsolete



    I admit that the way I have done it (double fork) is partially for historic reasons but at the time I did some quite intensive read-up on best-practice and this is what I then came up with (a decade or so ago)









    share























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Can systemd handle double-fork daemons ?



      I previously used init.d/ script to manage daemons I wrote which have worked flawless for years. Now I find myself needing to provide a systemd/ script for and I cannot get that to work with the way I do daemon setup code.



      When I setup a daemon I fork twice to loose controlling terminal and avoid being session leader.



      The problem with this is that I have confirmed that when you do that systemd/ looses track and kills the daemon. I have also confirmed that skipping the second fork makes it work again with systemd/.



      My basic (slightly simplified for the purpose of this discussion) systemd/ script is



      [Unit]
      Description=GM7 Service Daemon

      [Service]
      Type=forking
      ExecStart=/usr/bin/g7ctrl

      [Install]
      WantedBy=multi-user.target


      So I guess my question is : Do I need to change the way I have done daemon setup code in C/C++ for the last decade or is there is options to get systemd/ to track a double-fork ?



      I haven't quite read-up on the inner workings if systemd itself spawns processes for each daemon it starts in which case my double-fork would indeed be obsolete



      I admit that the way I have done it (double fork) is partially for historic reasons but at the time I did some quite intensive read-up on best-practice and this is what I then came up with (a decade or so ago)









      share













      Can systemd handle double-fork daemons ?



      I previously used init.d/ script to manage daemons I wrote which have worked flawless for years. Now I find myself needing to provide a systemd/ script for and I cannot get that to work with the way I do daemon setup code.



      When I setup a daemon I fork twice to loose controlling terminal and avoid being session leader.



      The problem with this is that I have confirmed that when you do that systemd/ looses track and kills the daemon. I have also confirmed that skipping the second fork makes it work again with systemd/.



      My basic (slightly simplified for the purpose of this discussion) systemd/ script is



      [Unit]
      Description=GM7 Service Daemon

      [Service]
      Type=forking
      ExecStart=/usr/bin/g7ctrl

      [Install]
      WantedBy=multi-user.target


      So I guess my question is : Do I need to change the way I have done daemon setup code in C/C++ for the last decade or is there is options to get systemd/ to track a double-fork ?



      I haven't quite read-up on the inner workings if systemd itself spawns processes for each daemon it starts in which case my double-fork would indeed be obsolete



      I admit that the way I have done it (double fork) is partially for historic reasons but at the time I did some quite intensive read-up on best-practice and this is what I then came up with (a decade or so ago)







      systemd daemon





      share












      share










      share



      share










      asked 9 mins ago









      Johan

      595




      595

























          active

          oldest

          votes











          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%2f476595%2fcan-systemd-handle-double-fork-daemons%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f476595%2fcan-systemd-handle-double-fork-daemons%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Peggy Mitchell

          Palaiologos

          The Forum (Inglewood, California)