How do I detach from a controlling terminal from the command line?












1















I know about nohup and it won't do what I want:



Example:



$ nohup sleep 600 2>/dev/null >/dev/null </dev/null&
[1] 21844
$ ps -ef | fgrep -e 'sleep
> TTY'
UID PID PPID C STIME TTY TIME CMD
me 21844 19313 0 09:37 pts/9 00:00:00 sleep 600


As you can see, sleep still has pts/9 as a controlling terminal. I don't want it to have any controlling terminal. Partly because the program I want to use (it isn't sleep if you haven't guessed) tries to open the controlling terminal to ask me questions and I want to see how it behaves if it can't. How do I make this happen?










share|improve this question





























    1















    I know about nohup and it won't do what I want:



    Example:



    $ nohup sleep 600 2>/dev/null >/dev/null </dev/null&
    [1] 21844
    $ ps -ef | fgrep -e 'sleep
    > TTY'
    UID PID PPID C STIME TTY TIME CMD
    me 21844 19313 0 09:37 pts/9 00:00:00 sleep 600


    As you can see, sleep still has pts/9 as a controlling terminal. I don't want it to have any controlling terminal. Partly because the program I want to use (it isn't sleep if you haven't guessed) tries to open the controlling terminal to ask me questions and I want to see how it behaves if it can't. How do I make this happen?










    share|improve this question



























      1












      1








      1


      1






      I know about nohup and it won't do what I want:



      Example:



      $ nohup sleep 600 2>/dev/null >/dev/null </dev/null&
      [1] 21844
      $ ps -ef | fgrep -e 'sleep
      > TTY'
      UID PID PPID C STIME TTY TIME CMD
      me 21844 19313 0 09:37 pts/9 00:00:00 sleep 600


      As you can see, sleep still has pts/9 as a controlling terminal. I don't want it to have any controlling terminal. Partly because the program I want to use (it isn't sleep if you haven't guessed) tries to open the controlling terminal to ask me questions and I want to see how it behaves if it can't. How do I make this happen?










      share|improve this question
















      I know about nohup and it won't do what I want:



      Example:



      $ nohup sleep 600 2>/dev/null >/dev/null </dev/null&
      [1] 21844
      $ ps -ef | fgrep -e 'sleep
      > TTY'
      UID PID PPID C STIME TTY TIME CMD
      me 21844 19313 0 09:37 pts/9 00:00:00 sleep 600


      As you can see, sleep still has pts/9 as a controlling terminal. I don't want it to have any controlling terminal. Partly because the program I want to use (it isn't sleep if you haven't guessed) tries to open the controlling terminal to ask me questions and I want to see how it behaves if it can't. How do I make this happen?







      linux bash job-control






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 18:14







      Omnifarious

















      asked Nov 14 '18 at 17:41









      OmnifariousOmnifarious

      450417




      450417






















          3 Answers
          3






          active

          oldest

          votes


















          1














          This is a FreeBSD solution, but perhaps a similar technique will work for your OS.



          I know cron isn't exactly the command line, but if you have a specific command list you want to run, cron can do that. You'll likely want to avoid having cron run the job repeatedly, perhaps by crafting a wrapper around your desired command list, something like:



          #!/bin/sh
          [ -f /tmp/my-semaphore-file ] || {
          touch /tmp/my-semaphore-file
          my_command_stack > /dev/null 2>&1
          }


          Inelegant perhaps for production use, but if you just want to test how your command stack performs with no controlling terminal, that will do it. The wrapper will not allow cron to run the command again until you:



          rm /tmp/my-semaphore-file


          at(1) is also an option, and is "almost" a command-line solution:



          echo 'my_command_stack > /dev/null 2>&1' | at now+1 minute





          share|improve this answer


























          • Even at now works. This is better than my "You can't do it." answer.

            – Omnifarious
            Nov 16 '18 at 16:07











          • A better answer to this question is setsid.

            – Omnifarious
            Jan 22 at 0:38



















          1














          The utility setsid on Linux can do this. On Fedora it's part of the util-linux package. This is the same package that contains things like mount, mkfs, /usr/bin/kill, and other similar things.






          share|improve this answer

































            0














            You need to also use disown to detach the process from the tty.



            https://unix.stackexchange.com/questions/3886/difference-between-nohup-disown-and



            Try running nohup sleep 600 2>/dev/null >/dev/null </dev/null& disown and see if that gives you the desired result.






            share|improve this answer
























            • Nope, sleep still has something in the TTY column. I did test it even though I didn't expect disown to have an effect. Dropping the controlling terminal is something that has to be done when a process is started AFAIK.

              – Omnifarious
              Nov 14 '18 at 18:40











            • Ah yes looking at this post it would appear that disown doesn't fix that after all: superuser.com/questions/1196406/…

              – zymhan
              Nov 14 '18 at 18:43











            • This little command line ditty results in a python3 process with no controlling terminal: sh -c 'nohup python3 -c "import os, time; time.sleep(5); os.setsid(); time.sleep(600)" &' </dev/null >/dev/null 2>/dev/null &

              – Omnifarious
              Nov 14 '18 at 18:56











            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "3"
            };
            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%2fsuperuser.com%2fquestions%2f1375395%2fhow-do-i-detach-from-a-controlling-terminal-from-the-command-line%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









            1














            This is a FreeBSD solution, but perhaps a similar technique will work for your OS.



            I know cron isn't exactly the command line, but if you have a specific command list you want to run, cron can do that. You'll likely want to avoid having cron run the job repeatedly, perhaps by crafting a wrapper around your desired command list, something like:



            #!/bin/sh
            [ -f /tmp/my-semaphore-file ] || {
            touch /tmp/my-semaphore-file
            my_command_stack > /dev/null 2>&1
            }


            Inelegant perhaps for production use, but if you just want to test how your command stack performs with no controlling terminal, that will do it. The wrapper will not allow cron to run the command again until you:



            rm /tmp/my-semaphore-file


            at(1) is also an option, and is "almost" a command-line solution:



            echo 'my_command_stack > /dev/null 2>&1' | at now+1 minute





            share|improve this answer


























            • Even at now works. This is better than my "You can't do it." answer.

              – Omnifarious
              Nov 16 '18 at 16:07











            • A better answer to this question is setsid.

              – Omnifarious
              Jan 22 at 0:38
















            1














            This is a FreeBSD solution, but perhaps a similar technique will work for your OS.



            I know cron isn't exactly the command line, but if you have a specific command list you want to run, cron can do that. You'll likely want to avoid having cron run the job repeatedly, perhaps by crafting a wrapper around your desired command list, something like:



            #!/bin/sh
            [ -f /tmp/my-semaphore-file ] || {
            touch /tmp/my-semaphore-file
            my_command_stack > /dev/null 2>&1
            }


            Inelegant perhaps for production use, but if you just want to test how your command stack performs with no controlling terminal, that will do it. The wrapper will not allow cron to run the command again until you:



            rm /tmp/my-semaphore-file


            at(1) is also an option, and is "almost" a command-line solution:



            echo 'my_command_stack > /dev/null 2>&1' | at now+1 minute





            share|improve this answer


























            • Even at now works. This is better than my "You can't do it." answer.

              – Omnifarious
              Nov 16 '18 at 16:07











            • A better answer to this question is setsid.

              – Omnifarious
              Jan 22 at 0:38














            1












            1








            1







            This is a FreeBSD solution, but perhaps a similar technique will work for your OS.



            I know cron isn't exactly the command line, but if you have a specific command list you want to run, cron can do that. You'll likely want to avoid having cron run the job repeatedly, perhaps by crafting a wrapper around your desired command list, something like:



            #!/bin/sh
            [ -f /tmp/my-semaphore-file ] || {
            touch /tmp/my-semaphore-file
            my_command_stack > /dev/null 2>&1
            }


            Inelegant perhaps for production use, but if you just want to test how your command stack performs with no controlling terminal, that will do it. The wrapper will not allow cron to run the command again until you:



            rm /tmp/my-semaphore-file


            at(1) is also an option, and is "almost" a command-line solution:



            echo 'my_command_stack > /dev/null 2>&1' | at now+1 minute





            share|improve this answer















            This is a FreeBSD solution, but perhaps a similar technique will work for your OS.



            I know cron isn't exactly the command line, but if you have a specific command list you want to run, cron can do that. You'll likely want to avoid having cron run the job repeatedly, perhaps by crafting a wrapper around your desired command list, something like:



            #!/bin/sh
            [ -f /tmp/my-semaphore-file ] || {
            touch /tmp/my-semaphore-file
            my_command_stack > /dev/null 2>&1
            }


            Inelegant perhaps for production use, but if you just want to test how your command stack performs with no controlling terminal, that will do it. The wrapper will not allow cron to run the command again until you:



            rm /tmp/my-semaphore-file


            at(1) is also an option, and is "almost" a command-line solution:



            echo 'my_command_stack > /dev/null 2>&1' | at now+1 minute






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 16 '18 at 0:50

























            answered Nov 16 '18 at 0:33









            Jim L.Jim L.

            37617




            37617













            • Even at now works. This is better than my "You can't do it." answer.

              – Omnifarious
              Nov 16 '18 at 16:07











            • A better answer to this question is setsid.

              – Omnifarious
              Jan 22 at 0:38



















            • Even at now works. This is better than my "You can't do it." answer.

              – Omnifarious
              Nov 16 '18 at 16:07











            • A better answer to this question is setsid.

              – Omnifarious
              Jan 22 at 0:38

















            Even at now works. This is better than my "You can't do it." answer.

            – Omnifarious
            Nov 16 '18 at 16:07





            Even at now works. This is better than my "You can't do it." answer.

            – Omnifarious
            Nov 16 '18 at 16:07













            A better answer to this question is setsid.

            – Omnifarious
            Jan 22 at 0:38





            A better answer to this question is setsid.

            – Omnifarious
            Jan 22 at 0:38













            1














            The utility setsid on Linux can do this. On Fedora it's part of the util-linux package. This is the same package that contains things like mount, mkfs, /usr/bin/kill, and other similar things.






            share|improve this answer






























              1














              The utility setsid on Linux can do this. On Fedora it's part of the util-linux package. This is the same package that contains things like mount, mkfs, /usr/bin/kill, and other similar things.






              share|improve this answer




























                1












                1








                1







                The utility setsid on Linux can do this. On Fedora it's part of the util-linux package. This is the same package that contains things like mount, mkfs, /usr/bin/kill, and other similar things.






                share|improve this answer















                The utility setsid on Linux can do this. On Fedora it's part of the util-linux package. This is the same package that contains things like mount, mkfs, /usr/bin/kill, and other similar things.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 22 at 0:37

























                answered Nov 15 '18 at 21:52









                OmnifariousOmnifarious

                450417




                450417























                    0














                    You need to also use disown to detach the process from the tty.



                    https://unix.stackexchange.com/questions/3886/difference-between-nohup-disown-and



                    Try running nohup sleep 600 2>/dev/null >/dev/null </dev/null& disown and see if that gives you the desired result.






                    share|improve this answer
























                    • Nope, sleep still has something in the TTY column. I did test it even though I didn't expect disown to have an effect. Dropping the controlling terminal is something that has to be done when a process is started AFAIK.

                      – Omnifarious
                      Nov 14 '18 at 18:40











                    • Ah yes looking at this post it would appear that disown doesn't fix that after all: superuser.com/questions/1196406/…

                      – zymhan
                      Nov 14 '18 at 18:43











                    • This little command line ditty results in a python3 process with no controlling terminal: sh -c 'nohup python3 -c "import os, time; time.sleep(5); os.setsid(); time.sleep(600)" &' </dev/null >/dev/null 2>/dev/null &

                      – Omnifarious
                      Nov 14 '18 at 18:56
















                    0














                    You need to also use disown to detach the process from the tty.



                    https://unix.stackexchange.com/questions/3886/difference-between-nohup-disown-and



                    Try running nohup sleep 600 2>/dev/null >/dev/null </dev/null& disown and see if that gives you the desired result.






                    share|improve this answer
























                    • Nope, sleep still has something in the TTY column. I did test it even though I didn't expect disown to have an effect. Dropping the controlling terminal is something that has to be done when a process is started AFAIK.

                      – Omnifarious
                      Nov 14 '18 at 18:40











                    • Ah yes looking at this post it would appear that disown doesn't fix that after all: superuser.com/questions/1196406/…

                      – zymhan
                      Nov 14 '18 at 18:43











                    • This little command line ditty results in a python3 process with no controlling terminal: sh -c 'nohup python3 -c "import os, time; time.sleep(5); os.setsid(); time.sleep(600)" &' </dev/null >/dev/null 2>/dev/null &

                      – Omnifarious
                      Nov 14 '18 at 18:56














                    0












                    0








                    0







                    You need to also use disown to detach the process from the tty.



                    https://unix.stackexchange.com/questions/3886/difference-between-nohup-disown-and



                    Try running nohup sleep 600 2>/dev/null >/dev/null </dev/null& disown and see if that gives you the desired result.






                    share|improve this answer













                    You need to also use disown to detach the process from the tty.



                    https://unix.stackexchange.com/questions/3886/difference-between-nohup-disown-and



                    Try running nohup sleep 600 2>/dev/null >/dev/null </dev/null& disown and see if that gives you the desired result.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 14 '18 at 18:38









                    zymhanzymhan

                    701513




                    701513













                    • Nope, sleep still has something in the TTY column. I did test it even though I didn't expect disown to have an effect. Dropping the controlling terminal is something that has to be done when a process is started AFAIK.

                      – Omnifarious
                      Nov 14 '18 at 18:40











                    • Ah yes looking at this post it would appear that disown doesn't fix that after all: superuser.com/questions/1196406/…

                      – zymhan
                      Nov 14 '18 at 18:43











                    • This little command line ditty results in a python3 process with no controlling terminal: sh -c 'nohup python3 -c "import os, time; time.sleep(5); os.setsid(); time.sleep(600)" &' </dev/null >/dev/null 2>/dev/null &

                      – Omnifarious
                      Nov 14 '18 at 18:56



















                    • Nope, sleep still has something in the TTY column. I did test it even though I didn't expect disown to have an effect. Dropping the controlling terminal is something that has to be done when a process is started AFAIK.

                      – Omnifarious
                      Nov 14 '18 at 18:40











                    • Ah yes looking at this post it would appear that disown doesn't fix that after all: superuser.com/questions/1196406/…

                      – zymhan
                      Nov 14 '18 at 18:43











                    • This little command line ditty results in a python3 process with no controlling terminal: sh -c 'nohup python3 -c "import os, time; time.sleep(5); os.setsid(); time.sleep(600)" &' </dev/null >/dev/null 2>/dev/null &

                      – Omnifarious
                      Nov 14 '18 at 18:56

















                    Nope, sleep still has something in the TTY column. I did test it even though I didn't expect disown to have an effect. Dropping the controlling terminal is something that has to be done when a process is started AFAIK.

                    – Omnifarious
                    Nov 14 '18 at 18:40





                    Nope, sleep still has something in the TTY column. I did test it even though I didn't expect disown to have an effect. Dropping the controlling terminal is something that has to be done when a process is started AFAIK.

                    – Omnifarious
                    Nov 14 '18 at 18:40













                    Ah yes looking at this post it would appear that disown doesn't fix that after all: superuser.com/questions/1196406/…

                    – zymhan
                    Nov 14 '18 at 18:43





                    Ah yes looking at this post it would appear that disown doesn't fix that after all: superuser.com/questions/1196406/…

                    – zymhan
                    Nov 14 '18 at 18:43













                    This little command line ditty results in a python3 process with no controlling terminal: sh -c 'nohup python3 -c "import os, time; time.sleep(5); os.setsid(); time.sleep(600)" &' </dev/null >/dev/null 2>/dev/null &

                    – Omnifarious
                    Nov 14 '18 at 18:56





                    This little command line ditty results in a python3 process with no controlling terminal: sh -c 'nohup python3 -c "import os, time; time.sleep(5); os.setsid(); time.sleep(600)" &' </dev/null >/dev/null 2>/dev/null &

                    – Omnifarious
                    Nov 14 '18 at 18:56


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Super User!


                    • 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%2fsuperuser.com%2fquestions%2f1375395%2fhow-do-i-detach-from-a-controlling-terminal-from-the-command-line%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?

                    Is this a new Fibonacci Identity?

                    19世紀