47

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.

2
  • From TTY1, Have you tried [command] > /dev/tty2 (you have to already logged-in to tty2)?
    – Pandya
    Commented Nov 26, 2014 at 13:54
  • 1
    this could be a XYproblem ... what are you trying to achieve? what is the overall goal? Commented Nov 27, 2014 at 7:48

6 Answers 6

43
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.

10
  • 3
    That is not correct. You mix up the controlling terminal with /dev/stdin, /dev/stdout, and /dev/stderr. ps easily shows that command does not have a controlling terminal at all in your case. Commented Nov 26, 2014 at 16:49
  • 1
    @HaukeLaging, a session leader takes control of a terminal as soon as it opens it. The problem was that the terminal device was not open by the session leader. Should be fixed now. Commented Nov 26, 2014 at 19:30
  • @StéphaneChazelas I tested with sleep 1000 and it still doesn't work here. I used a pseudo tty, though (shouldn't make a difference, I guess). Commented Nov 26, 2014 at 19:38
  • @HaukeLaging How exactly did you do it? Did that terminal not have a controlling session already (the As long as nothing else is using the other TTY part)? Commented Nov 26, 2014 at 19:41
  • 1
    @StéphaneChazelas I ran setsid sh -c 'exec sleep 1000 <>/dev/pts/4 >&0 2>&1' in a terminal emulator window. /dev/pts/4 is another terminal emulator window (same user, with bash running). Commented Nov 26, 2014 at 19:45
7

On the second tty there will be normally a program running, either some login program or some shell like bash. If you want interaction you either would have to replace the login program with yours, or tell a shell to run the program as if the program was started from the commandline.

A more simple solution, IMO, would be to start a tmux session after logging into the second screen and then use:

tmux send yourcommand ENTER

to start the program in the tmux session which will display after you switch to the second terminal.

7

I just made a discovery:

How can I launch applications from 2 ttys on launch?

One of the comments mentions something called openvt. This command appears to do the exact thing I'm after!

http://linux.about.com/library/cmd/blcmdl1_openvt.htm

Unless anyone knows different, I think this is probably the "correct" way to do this.

(I just tried it, and it seems to work fine - even though getty is running, it picks the next unused terminal. I guess VTs don't get "opened" until you switch to one to try to log in...)

0
4

i start a new graphical session on the vt5 with the follow command

xinit "/usr/bin/<binary_executable>" -- :1 vt5

for example :

xinit "/usr/bin/playonlinux" -- :1 vt5

If you want to launch a graphical application on already active graphical session you can do with :

DISPLAY=:0 "/usr/bin/playonlinux"
1
  • Thanks! this is exactly what I was trying to figure out (DISPLAY=:0)
    – Don
    Commented Jan 2, 2022 at 4:32
1

@wurtel's answer works as expected, but is not the best approach to do this. It depends on the legacy System V behavior, System V gains the the first tty opened by the process becomes its controlling tty (if the tty is not owned by another progress).

BSD and Linux have an explicit system call (via ioctl) to do this.

In order to launch a program on giving tty, setsid has a -c option to support this. For example, we want to run bash on /dev/tty2, we can just do:

setsid -c /usr/bin/bash </dev/tty2 >/dev/tty2 2>&1

Here, -c told setsid to take the ownership of stdin tty. And then setsid will transfer the owneship of tty to the final program.

For more details:

0

start bash on different tty:

setsid agetty --autologin root  --noclear 19200 ttyS1 linux

You must log in to answer this question.

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