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

3

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?

Omnifarious

Posted 2018-11-14T17:41:13.123

Reputation: 538

Answers

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

Jim L.

Posted 2018-11-14T17:41:13.123

Reputation: 669

Even at now works. This is better than my "You can't do it." answer. – Omnifarious – 2018-11-16T16:07:55.163

A better answer to this question is setsid. – Omnifarious – 2019-01-22T00:38:34.163

2

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.

Omnifarious

Posted 2018-11-14T17:41:13.123

Reputation: 538

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.

zymhan

Posted 2018-11-14T17:41:13.123

Reputation: 749

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 – 2018-11-14T18:40:44.917

Ah yes looking at this post it would appear that disown doesn't fix that after all: https://superuser.com/questions/1196406/linux-view-and-kill-disowned-process

– zymhan – 2018-11-14T18:43:59.843

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 – 2018-11-14T18:56:28.603