Bash – Start a process on a different tty

bashtty

After about an hour of Googling this, I can't believe nobody has actually asked this question before…

So I've got a script running on TTY1. How do I make that script launch some arbitrary program on TTY2?

  • I found tty, which tells you which TTY you're currently on.
  • I found writevt, which writes a single line of text onto a different TTY.
  • I found chvt, which changes which TTY is currently displayed.

I don't want to display TTY2. I just want the main script to continue executing normally, but if I manually switch to TTY2 I can interact with the second program.

Best Answer

setsid sh -c 'exec command <> /dev/tty2 >&0 2>&1'

As long as nothing else is using the other TTY (/dev/tty2 in this example), this should work. This includes a getty process that may be waiting for someone to login; having more than one process reading its input from a TTY will lead to unexpected results.

setsid takes care of starting the command in a new session.

Note that command will have to take care of setting the stty settings correctly, e.g. turn on "cooked mode" and onlcr so that outputting a newline will add a carriage return, etc.

Related Question