Terminal – How to Invoke a Command or Script Disconnected from the Controlling Terminal

controlling-terminalinteractiveprocessterminaltty

I'm investigating the behaviour of a script that is normally run as an automated process (e.g. cron, Jenkins). The script can (eventually) invoke commands that behave differently (seeking user input) when run interactively; for example, patch will ask what to do with a reversed patch, and svn will ask for passwords, but I need to see what happens when they're run non-interactively.

Persuading patch that it's non-interactive is fairly easy; I just need to redirect stdout to be a non-tty:

$ </dev/null > >(cat) /path/to/myscript --args

However svn will connect to the controlling terminal if it exists; editing the scripts to pass --non-interactive isn't really an option as this is coming from several levels deep and it'd be difficult to be certain I'd found every invocation.

Is there a way to invoke a script/command non-interactively, without a controlling terminal (so that /dev/tty doesn't exist)? I'd prefer stdout/stderr to still go to my terminal.

(I found the question Run script in a non interactive shell? but the answers to that discuss the differences between the cron and user environment; I've already eliminated all differences except non-interactivity.)

Best Answer

You need to start another session not attached to a terminal, so for instance:

$ setsid sh -c 'tty; ps -jp "$$"; echo test' < /dev/null > log 2>&1
$ cat log
not a tty
  PID  PGID   SID TTY          TIME CMD
19506 19506 19506 ?        00:00:00 sh
test

See also the start-stop-daemon command found on some Linux distributions. There's also a daemon command.