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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
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.
Method 3
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…)
Method 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"
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0