How can I run detached command with sudo over ssh?












1















I want to run some script through ssh and detach it, so it run even after ssh session is closed. I need to do this with sudo.



I can run some script but I am not able to detach it.
I tried following commands:



ssh 10.0.139.120 -t "sudo -b nohup some_script"


and



ssh 10.0.139.120 -t "sudo nohup some_script &"


Neither of above worked. Every time I get communicate "Connection to 10.0.139.171 closed." and no process is running on server.



I have also tried to do it with screen:



ssh 10.0.139.120 -t "sudo screen -S script -md some_script"


It did not work either, even though command works when I enter it on server locally.
How can I do it then?










share|improve this question

























  • Do you mean the script needs to be run with sudo? I’d try: ssh 10.0.139.120 -t 'sudo sh -c "nohup some_script &"'

    – dessert
    Jan 18 at 12:40











  • I have managed to do it with tmux, but I really would like to know why can'I I do it other way.

    – sbagin13
    Jan 18 at 13:07











  • unix.stackexchange.com/questions/30400/…

    – Simon Sudler
    Jan 18 at 13:28











  • Thanks, I saw that already, but I don't know why this solution doesn't work for me.

    – sbagin13
    Jan 18 at 13:38






  • 1





    It would really help if you could edit your question and explain how each of these "didn't work". Did the command not run at all? Did it run but was stopped when you disconnected? Something else? Telling us how they failed will let us understand why they failed and possibly answer you.

    – terdon
    Jan 18 at 15:21
















1















I want to run some script through ssh and detach it, so it run even after ssh session is closed. I need to do this with sudo.



I can run some script but I am not able to detach it.
I tried following commands:



ssh 10.0.139.120 -t "sudo -b nohup some_script"


and



ssh 10.0.139.120 -t "sudo nohup some_script &"


Neither of above worked. Every time I get communicate "Connection to 10.0.139.171 closed." and no process is running on server.



I have also tried to do it with screen:



ssh 10.0.139.120 -t "sudo screen -S script -md some_script"


It did not work either, even though command works when I enter it on server locally.
How can I do it then?










share|improve this question

























  • Do you mean the script needs to be run with sudo? I’d try: ssh 10.0.139.120 -t 'sudo sh -c "nohup some_script &"'

    – dessert
    Jan 18 at 12:40











  • I have managed to do it with tmux, but I really would like to know why can'I I do it other way.

    – sbagin13
    Jan 18 at 13:07











  • unix.stackexchange.com/questions/30400/…

    – Simon Sudler
    Jan 18 at 13:28











  • Thanks, I saw that already, but I don't know why this solution doesn't work for me.

    – sbagin13
    Jan 18 at 13:38






  • 1





    It would really help if you could edit your question and explain how each of these "didn't work". Did the command not run at all? Did it run but was stopped when you disconnected? Something else? Telling us how they failed will let us understand why they failed and possibly answer you.

    – terdon
    Jan 18 at 15:21














1












1








1








I want to run some script through ssh and detach it, so it run even after ssh session is closed. I need to do this with sudo.



I can run some script but I am not able to detach it.
I tried following commands:



ssh 10.0.139.120 -t "sudo -b nohup some_script"


and



ssh 10.0.139.120 -t "sudo nohup some_script &"


Neither of above worked. Every time I get communicate "Connection to 10.0.139.171 closed." and no process is running on server.



I have also tried to do it with screen:



ssh 10.0.139.120 -t "sudo screen -S script -md some_script"


It did not work either, even though command works when I enter it on server locally.
How can I do it then?










share|improve this question
















I want to run some script through ssh and detach it, so it run even after ssh session is closed. I need to do this with sudo.



I can run some script but I am not able to detach it.
I tried following commands:



ssh 10.0.139.120 -t "sudo -b nohup some_script"


and



ssh 10.0.139.120 -t "sudo nohup some_script &"


Neither of above worked. Every time I get communicate "Connection to 10.0.139.171 closed." and no process is running on server.



I have also tried to do it with screen:



ssh 10.0.139.120 -t "sudo screen -S script -md some_script"


It did not work either, even though command works when I enter it on server locally.
How can I do it then?







command-line ssh screen






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 22 at 16:01







sbagin13

















asked Jan 18 at 12:37









sbagin13sbagin13

11816




11816













  • Do you mean the script needs to be run with sudo? I’d try: ssh 10.0.139.120 -t 'sudo sh -c "nohup some_script &"'

    – dessert
    Jan 18 at 12:40











  • I have managed to do it with tmux, but I really would like to know why can'I I do it other way.

    – sbagin13
    Jan 18 at 13:07











  • unix.stackexchange.com/questions/30400/…

    – Simon Sudler
    Jan 18 at 13:28











  • Thanks, I saw that already, but I don't know why this solution doesn't work for me.

    – sbagin13
    Jan 18 at 13:38






  • 1





    It would really help if you could edit your question and explain how each of these "didn't work". Did the command not run at all? Did it run but was stopped when you disconnected? Something else? Telling us how they failed will let us understand why they failed and possibly answer you.

    – terdon
    Jan 18 at 15:21



















  • Do you mean the script needs to be run with sudo? I’d try: ssh 10.0.139.120 -t 'sudo sh -c "nohup some_script &"'

    – dessert
    Jan 18 at 12:40











  • I have managed to do it with tmux, but I really would like to know why can'I I do it other way.

    – sbagin13
    Jan 18 at 13:07











  • unix.stackexchange.com/questions/30400/…

    – Simon Sudler
    Jan 18 at 13:28











  • Thanks, I saw that already, but I don't know why this solution doesn't work for me.

    – sbagin13
    Jan 18 at 13:38






  • 1





    It would really help if you could edit your question and explain how each of these "didn't work". Did the command not run at all? Did it run but was stopped when you disconnected? Something else? Telling us how they failed will let us understand why they failed and possibly answer you.

    – terdon
    Jan 18 at 15:21

















Do you mean the script needs to be run with sudo? I’d try: ssh 10.0.139.120 -t 'sudo sh -c "nohup some_script &"'

– dessert
Jan 18 at 12:40





Do you mean the script needs to be run with sudo? I’d try: ssh 10.0.139.120 -t 'sudo sh -c "nohup some_script &"'

– dessert
Jan 18 at 12:40













I have managed to do it with tmux, but I really would like to know why can'I I do it other way.

– sbagin13
Jan 18 at 13:07





I have managed to do it with tmux, but I really would like to know why can'I I do it other way.

– sbagin13
Jan 18 at 13:07













unix.stackexchange.com/questions/30400/…

– Simon Sudler
Jan 18 at 13:28





unix.stackexchange.com/questions/30400/…

– Simon Sudler
Jan 18 at 13:28













Thanks, I saw that already, but I don't know why this solution doesn't work for me.

– sbagin13
Jan 18 at 13:38





Thanks, I saw that already, but I don't know why this solution doesn't work for me.

– sbagin13
Jan 18 at 13:38




1




1





It would really help if you could edit your question and explain how each of these "didn't work". Did the command not run at all? Did it run but was stopped when you disconnected? Something else? Telling us how they failed will let us understand why they failed and possibly answer you.

– terdon
Jan 18 at 15:21





It would really help if you could edit your question and explain how each of these "didn't work". Did the command not run at all? Did it run but was stopped when you disconnected? Something else? Telling us how they failed will let us understand why they failed and possibly answer you.

– terdon
Jan 18 at 15:21










2 Answers
2






active

oldest

votes


















1














I did small survey and found this similar question on Stack Overflow. At all you must add path to the script or must go inside the directory, where the script is located:





ssh remote-host -t "sudo -b sh -c 'nohup /path/some_script > /dev/null 2>&1 &'"




ssh remote-host -t "sudo -b sh -c 'cd /path; nohup ./some_script > /dev/null 2>&1 &'"


Without sh -c or bash -c the ampersand & at the end breaks the command in some way.





I couldn't manage to use disown for this purpose, but setsid works well:



 ssh remote-host -t 'sudo -b setsid /path/some_script'





share|improve this answer

































    0














    You can try using disown to avoid the process being killed after you close you ssh session.






    share|improve this answer























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "89"
      };
      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: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      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%2faskubuntu.com%2fquestions%2f1110865%2fhow-can-i-run-detached-command-with-sudo-over-ssh%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      I did small survey and found this similar question on Stack Overflow. At all you must add path to the script or must go inside the directory, where the script is located:





      ssh remote-host -t "sudo -b sh -c 'nohup /path/some_script > /dev/null 2>&1 &'"




      ssh remote-host -t "sudo -b sh -c 'cd /path; nohup ./some_script > /dev/null 2>&1 &'"


      Without sh -c or bash -c the ampersand & at the end breaks the command in some way.





      I couldn't manage to use disown for this purpose, but setsid works well:



       ssh remote-host -t 'sudo -b setsid /path/some_script'





      share|improve this answer






























        1














        I did small survey and found this similar question on Stack Overflow. At all you must add path to the script or must go inside the directory, where the script is located:





        ssh remote-host -t "sudo -b sh -c 'nohup /path/some_script > /dev/null 2>&1 &'"




        ssh remote-host -t "sudo -b sh -c 'cd /path; nohup ./some_script > /dev/null 2>&1 &'"


        Without sh -c or bash -c the ampersand & at the end breaks the command in some way.





        I couldn't manage to use disown for this purpose, but setsid works well:



         ssh remote-host -t 'sudo -b setsid /path/some_script'





        share|improve this answer




























          1












          1








          1







          I did small survey and found this similar question on Stack Overflow. At all you must add path to the script or must go inside the directory, where the script is located:





          ssh remote-host -t "sudo -b sh -c 'nohup /path/some_script > /dev/null 2>&1 &'"




          ssh remote-host -t "sudo -b sh -c 'cd /path; nohup ./some_script > /dev/null 2>&1 &'"


          Without sh -c or bash -c the ampersand & at the end breaks the command in some way.





          I couldn't manage to use disown for this purpose, but setsid works well:



           ssh remote-host -t 'sudo -b setsid /path/some_script'





          share|improve this answer















          I did small survey and found this similar question on Stack Overflow. At all you must add path to the script or must go inside the directory, where the script is located:





          ssh remote-host -t "sudo -b sh -c 'nohup /path/some_script > /dev/null 2>&1 &'"




          ssh remote-host -t "sudo -b sh -c 'cd /path; nohup ./some_script > /dev/null 2>&1 &'"


          Without sh -c or bash -c the ampersand & at the end breaks the command in some way.





          I couldn't manage to use disown for this purpose, but setsid works well:



           ssh remote-host -t 'sudo -b setsid /path/some_script'






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 24 at 10:04

























          answered Jan 22 at 17:12









          pa4080pa4080

          14.1k52564




          14.1k52564

























              0














              You can try using disown to avoid the process being killed after you close you ssh session.






              share|improve this answer




























                0














                You can try using disown to avoid the process being killed after you close you ssh session.






                share|improve this answer


























                  0












                  0








                  0







                  You can try using disown to avoid the process being killed after you close you ssh session.






                  share|improve this answer













                  You can try using disown to avoid the process being killed after you close you ssh session.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 22 at 17:39









                  Joan Marcè i IgualJoan Marcè i Igual

                  11




                  11






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Ask Ubuntu!


                      • 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%2faskubuntu.com%2fquestions%2f1110865%2fhow-can-i-run-detached-command-with-sudo-over-ssh%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

                      How to make a Squid Proxy server?

                      第一次世界大戦

                      Touch on Surface Book