Systemd - How to restart a node process from userspace that uses passwordless sudo












0















I have a nodejs process that runs in user space (and needs to for a reason) but it has one dependency that requires root access for the raspberry pi gpio interrupts. So the solution is I have have a group gpio on the rpi with a sudoers entry for running node. Then I fire this script up with a service unit file and it works fine



[Unit]
Description=switches16-mcp230xx-dSxJM-interrupt Device Starter Service
After=network-online.target

[Service]
Environment=UCI_ENV=pro
ExecStart=/usr/bin/sudo /usr/bin/node -r esm /opt/light/switches16-mcp230xx-dSxJM-interrupt/index.js
Restart=on-failure
WorkingDirectory=/opt/light/switches16-mcp230xx-dSxJM-interrupt

[Install]
WantedBy=default.target


The problem comes on restart (or failure restart). The process needs to be killed but it was started as sudo so not surprisingly it must be killed by using sudo as well.



Mar 12 19:43:12 switches systemd[1289]: switches16-mcp230xx-dSxJM-interrupt.service: Failed to kill main process 11222 (sudo), ignoring: Operation not permitted
Mar 12 19:43:12 switches systemd[1289]: switches16-mcp230xx-dSxJM-interrupt.service: Killing process 11223 (node) with signal SIGKILL.
Mar 12 19:43:12 switches systemd[1289]: switches16-mcp230xx-dSxJM-interrupt.service: Failed to kill control group /user.slice/user-1000.slice/user@1000.service/switches16-mcp230xx-dSxJM-


So since the unit file is launched in userspace it kills in userspace so I assumed I must deliberately kill the process using sudo as I started it. so I added this.



ExecReload=/usr/bin/sudo /bin/kill -HUP $MAINPID



but got the same error. Even though my sudoers.d file has



%gpio ALL=NOPASSWD: /usr/bin/node
%gpio ALL=NOPASSWD: /bin/kill


I must be missing something here but I can't put my finger on it.



here are the processes that unit file spawns that must be killed before restart



root     11093  0.0  0.3   8468  2920 ?        Ss   18:01   0:00  _ /usr/bin/sudo /usr/bin/node -r esm /opt/light/switches16-mcp230xx-dSxJM-interrupt/index.js
root 11094 6.7 4.8 142820 45788 ? SLl 18:01 6:52 _ /usr/bin/node -r esm /opt/light/switches16-mcp230xx-dSxJM-interrupt/index.js


Any suggestions?



Let me reiterate that I can't run this with the service file in root space so no point in suggesting that as it would obviously be the easy answer. I am deploying this code and starting via ssh without a root access key so running in userspace with passwordless sudo is my only option. If not for this particular hardware (root) dependency in my code I won't be faced with this issue at all.









share



























    0















    I have a nodejs process that runs in user space (and needs to for a reason) but it has one dependency that requires root access for the raspberry pi gpio interrupts. So the solution is I have have a group gpio on the rpi with a sudoers entry for running node. Then I fire this script up with a service unit file and it works fine



    [Unit]
    Description=switches16-mcp230xx-dSxJM-interrupt Device Starter Service
    After=network-online.target

    [Service]
    Environment=UCI_ENV=pro
    ExecStart=/usr/bin/sudo /usr/bin/node -r esm /opt/light/switches16-mcp230xx-dSxJM-interrupt/index.js
    Restart=on-failure
    WorkingDirectory=/opt/light/switches16-mcp230xx-dSxJM-interrupt

    [Install]
    WantedBy=default.target


    The problem comes on restart (or failure restart). The process needs to be killed but it was started as sudo so not surprisingly it must be killed by using sudo as well.



    Mar 12 19:43:12 switches systemd[1289]: switches16-mcp230xx-dSxJM-interrupt.service: Failed to kill main process 11222 (sudo), ignoring: Operation not permitted
    Mar 12 19:43:12 switches systemd[1289]: switches16-mcp230xx-dSxJM-interrupt.service: Killing process 11223 (node) with signal SIGKILL.
    Mar 12 19:43:12 switches systemd[1289]: switches16-mcp230xx-dSxJM-interrupt.service: Failed to kill control group /user.slice/user-1000.slice/user@1000.service/switches16-mcp230xx-dSxJM-


    So since the unit file is launched in userspace it kills in userspace so I assumed I must deliberately kill the process using sudo as I started it. so I added this.



    ExecReload=/usr/bin/sudo /bin/kill -HUP $MAINPID



    but got the same error. Even though my sudoers.d file has



    %gpio ALL=NOPASSWD: /usr/bin/node
    %gpio ALL=NOPASSWD: /bin/kill


    I must be missing something here but I can't put my finger on it.



    here are the processes that unit file spawns that must be killed before restart



    root     11093  0.0  0.3   8468  2920 ?        Ss   18:01   0:00  _ /usr/bin/sudo /usr/bin/node -r esm /opt/light/switches16-mcp230xx-dSxJM-interrupt/index.js
    root 11094 6.7 4.8 142820 45788 ? SLl 18:01 6:52 _ /usr/bin/node -r esm /opt/light/switches16-mcp230xx-dSxJM-interrupt/index.js


    Any suggestions?



    Let me reiterate that I can't run this with the service file in root space so no point in suggesting that as it would obviously be the easy answer. I am deploying this code and starting via ssh without a root access key so running in userspace with passwordless sudo is my only option. If not for this particular hardware (root) dependency in my code I won't be faced with this issue at all.









    share

























      0












      0








      0








      I have a nodejs process that runs in user space (and needs to for a reason) but it has one dependency that requires root access for the raspberry pi gpio interrupts. So the solution is I have have a group gpio on the rpi with a sudoers entry for running node. Then I fire this script up with a service unit file and it works fine



      [Unit]
      Description=switches16-mcp230xx-dSxJM-interrupt Device Starter Service
      After=network-online.target

      [Service]
      Environment=UCI_ENV=pro
      ExecStart=/usr/bin/sudo /usr/bin/node -r esm /opt/light/switches16-mcp230xx-dSxJM-interrupt/index.js
      Restart=on-failure
      WorkingDirectory=/opt/light/switches16-mcp230xx-dSxJM-interrupt

      [Install]
      WantedBy=default.target


      The problem comes on restart (or failure restart). The process needs to be killed but it was started as sudo so not surprisingly it must be killed by using sudo as well.



      Mar 12 19:43:12 switches systemd[1289]: switches16-mcp230xx-dSxJM-interrupt.service: Failed to kill main process 11222 (sudo), ignoring: Operation not permitted
      Mar 12 19:43:12 switches systemd[1289]: switches16-mcp230xx-dSxJM-interrupt.service: Killing process 11223 (node) with signal SIGKILL.
      Mar 12 19:43:12 switches systemd[1289]: switches16-mcp230xx-dSxJM-interrupt.service: Failed to kill control group /user.slice/user-1000.slice/user@1000.service/switches16-mcp230xx-dSxJM-


      So since the unit file is launched in userspace it kills in userspace so I assumed I must deliberately kill the process using sudo as I started it. so I added this.



      ExecReload=/usr/bin/sudo /bin/kill -HUP $MAINPID



      but got the same error. Even though my sudoers.d file has



      %gpio ALL=NOPASSWD: /usr/bin/node
      %gpio ALL=NOPASSWD: /bin/kill


      I must be missing something here but I can't put my finger on it.



      here are the processes that unit file spawns that must be killed before restart



      root     11093  0.0  0.3   8468  2920 ?        Ss   18:01   0:00  _ /usr/bin/sudo /usr/bin/node -r esm /opt/light/switches16-mcp230xx-dSxJM-interrupt/index.js
      root 11094 6.7 4.8 142820 45788 ? SLl 18:01 6:52 _ /usr/bin/node -r esm /opt/light/switches16-mcp230xx-dSxJM-interrupt/index.js


      Any suggestions?



      Let me reiterate that I can't run this with the service file in root space so no point in suggesting that as it would obviously be the easy answer. I am deploying this code and starting via ssh without a root access key so running in userspace with passwordless sudo is my only option. If not for this particular hardware (root) dependency in my code I won't be faced with this issue at all.









      share














      I have a nodejs process that runs in user space (and needs to for a reason) but it has one dependency that requires root access for the raspberry pi gpio interrupts. So the solution is I have have a group gpio on the rpi with a sudoers entry for running node. Then I fire this script up with a service unit file and it works fine



      [Unit]
      Description=switches16-mcp230xx-dSxJM-interrupt Device Starter Service
      After=network-online.target

      [Service]
      Environment=UCI_ENV=pro
      ExecStart=/usr/bin/sudo /usr/bin/node -r esm /opt/light/switches16-mcp230xx-dSxJM-interrupt/index.js
      Restart=on-failure
      WorkingDirectory=/opt/light/switches16-mcp230xx-dSxJM-interrupt

      [Install]
      WantedBy=default.target


      The problem comes on restart (or failure restart). The process needs to be killed but it was started as sudo so not surprisingly it must be killed by using sudo as well.



      Mar 12 19:43:12 switches systemd[1289]: switches16-mcp230xx-dSxJM-interrupt.service: Failed to kill main process 11222 (sudo), ignoring: Operation not permitted
      Mar 12 19:43:12 switches systemd[1289]: switches16-mcp230xx-dSxJM-interrupt.service: Killing process 11223 (node) with signal SIGKILL.
      Mar 12 19:43:12 switches systemd[1289]: switches16-mcp230xx-dSxJM-interrupt.service: Failed to kill control group /user.slice/user-1000.slice/user@1000.service/switches16-mcp230xx-dSxJM-


      So since the unit file is launched in userspace it kills in userspace so I assumed I must deliberately kill the process using sudo as I started it. so I added this.



      ExecReload=/usr/bin/sudo /bin/kill -HUP $MAINPID



      but got the same error. Even though my sudoers.d file has



      %gpio ALL=NOPASSWD: /usr/bin/node
      %gpio ALL=NOPASSWD: /bin/kill


      I must be missing something here but I can't put my finger on it.



      here are the processes that unit file spawns that must be killed before restart



      root     11093  0.0  0.3   8468  2920 ?        Ss   18:01   0:00  _ /usr/bin/sudo /usr/bin/node -r esm /opt/light/switches16-mcp230xx-dSxJM-interrupt/index.js
      root 11094 6.7 4.8 142820 45788 ? SLl 18:01 6:52 _ /usr/bin/node -r esm /opt/light/switches16-mcp230xx-dSxJM-interrupt/index.js


      Any suggestions?



      Let me reiterate that I can't run this with the service file in root space so no point in suggesting that as it would obviously be the easy answer. I am deploying this code and starting via ssh without a root access key so running in userspace with passwordless sudo is my only option. If not for this particular hardware (root) dependency in my code I won't be faced with this issue at all.







      systemd





      share












      share










      share



      share










      asked 4 mins ago









      DKeblerDKebler

      16018




      16018






















          0






          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',
          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%2f506001%2fsystemd-how-to-restart-a-node-process-from-userspace-that-uses-passwordless-su%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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%2f506001%2fsystemd-how-to-restart-a-node-process-from-userspace-that-uses-passwordless-su%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

          サソリ

          広島県道265号伴広島線

          Setup Asymptote in Texstudio