5

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?

3 Answers 3

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
3
  • Even at now works. This is better than my "You can't do it." answer. Commented Nov 16, 2018 at 16:07
  • 1
    A better answer to this question is setsid. Commented Jan 22, 2019 at 0:38
  • @Omnifarious: Consider marking your updated answer as the accepted answer.
    – tom
    Commented Jul 16, 2022 at 10:22
7

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.

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.

3
  • 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. Commented Nov 14, 2018 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
    Commented Nov 14, 2018 at 18:43
  • 1
    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 & Commented Nov 14, 2018 at 18:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .