3

If I run a Bash script via the setsid command, its controlling terminal will not be attached to any device (it will show up as "?"). Is there a way to re-attach the controlling terminal to some free pts device from a shell script.

Will I be able to use stdin and stdout inside the script run via setsid, after its controlling terminal gets detached?

Tomek
  • 621
  • 1
  • 6
  • 16
  • 2
    There is no *easy* way AFAIK. Take a look [here](https://blog.nelhage.com/2011/02/changing-ctty/) – Eugene Sh. Jul 17 '15 at 14:59
  • 2
    maybe this? https://github.com/nelhage/reptyr – jon Jul 17 '15 at 16:25
  • 1
    Of course you can use stdin and stdout. But I think you intend to ask "can I make the stdin of the script be associated with my keyboard, and the stdout be associated with a tty?" – William Pursell Jul 17 '15 at 17:23
  • @WilliamPursell - yes, that is exactly what I mean. Isn't that going to be the case that after starting the new session, stdin and stdout will still be working on the current terminal, I just won't be able to send the control signals? What I am looking for is a way to run commands in a separate sessions so that they can easily be all terminated; but at the same time, I wouldn't like to change the way these commands are run by, e.g. disabling stdin keyboard access. – Tomek Jul 18 '15 at 11:14
  • 1
    You can send a signal to every process in the group using kill. From the man page: ` Negative PID values may be used to choose whole process groups`. If you want to be able to do that with ctrl-C, keep your shell script in the foreground, run everything else in a setsid'd subshell and trap SIGTERM to send the signal to the group. – William Pursell Jul 19 '15 at 13:51

0 Answers0