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











up vote
1
down vote

favorite
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




























    up vote
    1
    down vote

    favorite
    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


























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      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 at 18:14

























      asked Nov 14 at 17:41









      Omnifarious

      430417




      430417






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          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 at 16:07


















          up vote
          0
          down vote













          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 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 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 at 18:56


















          up vote
          0
          down vote













          As far as I can tell, the answer to this question is that you can't do this from the command line on most Linux distributions.



          Doing this in Unix/POSIX/Linux in general is possible, but requires a very specific sequence of system calls involving multiple processes. And an explanation of why and how would require explaining things at the code level, which I don't think is appropriate for superuser.



          And while there used to be command lines tools that would do this (I think daemonize was one of them) they do not seem to be widely distributed anymore. At least, Fedora doesn't package them. One of the reasons why is likely because this has interesting implications from the standpoint of things like systemd which have a notion of a user session quite apart from the process groups and session ids that traditional terminal oriented job control has typically relied on.






          share|improve this answer





















            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',
            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








            up vote
            1
            down vote



            accepted










            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 at 16:07















            up vote
            1
            down vote



            accepted










            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 at 16:07













            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            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 at 0:50

























            answered Nov 16 at 0:33









            Jim L.

            663




            663












            • Even at now works. This is better than my "You can't do it." answer.
              – Omnifarious
              Nov 16 at 16:07


















            • Even at now works. This is better than my "You can't do it." answer.
              – Omnifarious
              Nov 16 at 16:07
















            Even at now works. This is better than my "You can't do it." answer.
            – Omnifarious
            Nov 16 at 16:07




            Even at now works. This is better than my "You can't do it." answer.
            – Omnifarious
            Nov 16 at 16:07












            up vote
            0
            down vote













            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 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 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 at 18:56















            up vote
            0
            down vote













            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 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 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 at 18:56













            up vote
            0
            down vote










            up vote
            0
            down vote









            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 at 18:38









            zymhan

            666513




            666513












            • 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 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 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 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 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 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 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 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 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 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 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 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 at 18:56










            up vote
            0
            down vote













            As far as I can tell, the answer to this question is that you can't do this from the command line on most Linux distributions.



            Doing this in Unix/POSIX/Linux in general is possible, but requires a very specific sequence of system calls involving multiple processes. And an explanation of why and how would require explaining things at the code level, which I don't think is appropriate for superuser.



            And while there used to be command lines tools that would do this (I think daemonize was one of them) they do not seem to be widely distributed anymore. At least, Fedora doesn't package them. One of the reasons why is likely because this has interesting implications from the standpoint of things like systemd which have a notion of a user session quite apart from the process groups and session ids that traditional terminal oriented job control has typically relied on.






            share|improve this answer

























              up vote
              0
              down vote













              As far as I can tell, the answer to this question is that you can't do this from the command line on most Linux distributions.



              Doing this in Unix/POSIX/Linux in general is possible, but requires a very specific sequence of system calls involving multiple processes. And an explanation of why and how would require explaining things at the code level, which I don't think is appropriate for superuser.



              And while there used to be command lines tools that would do this (I think daemonize was one of them) they do not seem to be widely distributed anymore. At least, Fedora doesn't package them. One of the reasons why is likely because this has interesting implications from the standpoint of things like systemd which have a notion of a user session quite apart from the process groups and session ids that traditional terminal oriented job control has typically relied on.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                As far as I can tell, the answer to this question is that you can't do this from the command line on most Linux distributions.



                Doing this in Unix/POSIX/Linux in general is possible, but requires a very specific sequence of system calls involving multiple processes. And an explanation of why and how would require explaining things at the code level, which I don't think is appropriate for superuser.



                And while there used to be command lines tools that would do this (I think daemonize was one of them) they do not seem to be widely distributed anymore. At least, Fedora doesn't package them. One of the reasons why is likely because this has interesting implications from the standpoint of things like systemd which have a notion of a user session quite apart from the process groups and session ids that traditional terminal oriented job control has typically relied on.






                share|improve this answer












                As far as I can tell, the answer to this question is that you can't do this from the command line on most Linux distributions.



                Doing this in Unix/POSIX/Linux in general is possible, but requires a very specific sequence of system calls involving multiple processes. And an explanation of why and how would require explaining things at the code level, which I don't think is appropriate for superuser.



                And while there used to be command lines tools that would do this (I think daemonize was one of them) they do not seem to be widely distributed anymore. At least, Fedora doesn't package them. One of the reasons why is likely because this has interesting implications from the standpoint of things like systemd which have a notion of a user session quite apart from the process groups and session ids that traditional terminal oriented job control has typically relied on.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 at 21:52









                Omnifarious

                430417




                430417






























                     

                    draft saved


                    draft discarded



















































                     


                    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

                    Сан-Квентин

                    Алькесар

                    Josef Freinademetz