You can send color/cursor escapes to a terminal by simply writing to /dev/pts/N
or /dev/ttyN
; for instance, if /dev/pts/5
is an xterm, you can set its background to red from anywhere with printf %b '\e]11;#ff0000\a' > /dev/pts/5
(of course, you need write permissions to /dev/pts/5
).
There is a slim chance that running such a command may badly interract with other escapes sent by the program(s) running in the terminal, but in the worst case this will only result in a scrambled terminal.
For the more general problem of running a program in another terminal/session, the only way I can think that possible is by hijacking a process running in that terminal, and fork+exec the program from inside it. Example:
gdb -p PID -batch -ex 'p system("ls")'
This gets a lot more involved if the program has to read from the terminal; before the exec, the process will have to be added to the foreground process group.
cat > /dev/pts/x <<EOF\n $(ls -l)\n EOF
? Where$(ls -l)
is in a new line? – Valentin Bajrami Oct 28 '18 at 09:12tty
instead ofls
and it printed the number of the local terminal. – dblouis Oct 28 '18 at 09:17ioctl
you're using to change the color of the terminal ;-) – Oct 28 '18 at 10:24TIOCSTI
. But that's still an interesting problem (both the one from the question, and the one from the comments). – Oct 28 '18 at 11:00foo
updates it for you, by emitting relevant escape sequences. Thenfoo > /dev/pts/x
should work for updating them in another terminal. (The prompt solution might also be a good one.) – egmont Oct 28 '18 at 11:12/dev/pts/N
or/dev/ttyN
; for instance, if/dev/pts/5
is an xterm, you can set its background to red from anywhere withprintf %b '\e]11;#ff0000\a' > /dev/pts/5
(of course, you need write permissions to/dev/pts/5
). – Oct 28 '18 at 12:16printf f > /dev/tty2
won't sendf
to the program running inside it. And theprintf
won't cut in the middle of another sequence unless that sequence was already split, ie the app used twowrite(2)
s to put it out. – Oct 28 '18 at 13:00ttyecho
does not run a command in another tty, it just fakes input withTIOCSTI
(which is quite dangerous). Whwn I get time, I will expand my ptrace/gdb hack into something more robust, no matter if this question is closed or not. – Oct 29 '18 at 12:19ioctl
to the pts”? You said “I am going to write an answer”; do you have an answer? Are willing to tell us what it is? – G-Man Says 'Reinstate Monica' Oct 29 '18 at 22:13