How do I detach from a controlling terminal from the command line?
up vote
1
down vote
favorite
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
add a comment |
up vote
1
down vote
favorite
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
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
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
linux bash job-control
edited Nov 14 at 18:14
asked Nov 14 at 17:41
Omnifarious
430417
430417
add a comment |
add a comment |
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
Evenat now
works. This is better than my "You can't do it." answer.
– Omnifarious
Nov 16 at 16:07
add a comment |
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.
Nope, sleep still has something in theTTY
column. I did test it even though I didn't expectdisown
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 apython3
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
add a comment |
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.
add a comment |
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
Evenat now
works. This is better than my "You can't do it." answer.
– Omnifarious
Nov 16 at 16:07
add a comment |
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
Evenat now
works. This is better than my "You can't do it." answer.
– Omnifarious
Nov 16 at 16:07
add a comment |
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
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
edited Nov 16 at 0:50
answered Nov 16 at 0:33
Jim L.
663
663
Evenat now
works. This is better than my "You can't do it." answer.
– Omnifarious
Nov 16 at 16:07
add a comment |
Evenat 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
add a comment |
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.
Nope, sleep still has something in theTTY
column. I did test it even though I didn't expectdisown
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 apython3
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
add a comment |
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.
Nope, sleep still has something in theTTY
column. I did test it even though I didn't expectdisown
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 apython3
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
add a comment |
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.
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.
answered Nov 14 at 18:38
zymhan
666513
666513
Nope, sleep still has something in theTTY
column. I did test it even though I didn't expectdisown
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 apython3
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
add a comment |
Nope, sleep still has something in theTTY
column. I did test it even though I didn't expectdisown
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 apython3
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 15 at 21:52
Omnifarious
430417
430417
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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