You are not logged in.

#1 2016-05-31 21:07:30

mdodkins
Member
Registered: 2016-05-31
Posts: 5

Executing commands on other ttys

Hi!

I am having trouble executing a command on one tty from another tty.

I can echo text no problem, but no "end of line" character seems to cause that text to be executed as though it's a command.

I've tried these steps:

sudo su
echo "clear" > /dev/ttyN

... with these newline characters:

\r
\r\r
\n
\r\n

But nothing seems to actually cause the command to be executed. I've also tried a screen session:

screen /dev/ttyN

... and I can also enter text via this program, but it doesn't actually execute the command when I hit enter.

I've also messed around with "screen -X" but it ain't doing any good!

Starting to get me down, so if anyone has any ideas please let me know. You can reproduce this easily by exiting a Window Manager and using ALT+F1, ALT+F2 etc. to create ttys.

Thanks!!

Offline

#2 2016-05-31 21:52:00

teckk
Member
Registered: 2013-02-21
Posts: 519

Re: Executing commands on other ttys

echo 'hi' > /dev/tty2

will echo "hi" to the screen of /dev/tty2

ls > /dev/tty2

will display ls output on /dev/tty2

for i in {1..20}; do echo $i > /dev/tty2; sleep 1; done

Will count 1 to 20 on /dev/tty2

Or are you wanting something else?

Offline

#3 2016-05-31 22:48:09

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,534
Website

Re: Executing commands on other ttys

mdodkins wrote:

I am having trouble executing a command on one tty from another tty.

You are starting from a fundamentally wrong approach based on a faulty assumption:

mdodkins wrote:

no "end of line" character seems to cause that text to be executed as though it's a command.

That's right, because it is not a command.  You are printing output to the terminal (a normal standard-output destination) you are not sending commands to a shell (normal target of keyboard standard-input).

All (most) programs have a stdin and stdout.  "Commands" are processed by a shell - commonly BASH.  BASH, like most programs, has a stdin and stdout.  In a common interactive session, BASH's stdin is the keyboard and BASH's stdout is the terminal screen:

Keyboard -> BASH -> screen

Now you are sending extra output to that same destination.  So it shows up there like output from BASH.  But it never goes *in* to BASH.

You could send output through a shell session to the other tty:

echo some-command-here | bash > /dev/tty2

But this is most likely not what you want.  This would invoke a *separate* bash shell that would *also* output to tty2 - this is not the same as sending a command to the bash shell already running in tty2.

You could send a command string to the stdin of the bash shell already running in tty2.  This would required some exploration to find the pid of that bash session, and sending the string to a fd under /proc.  This is error-prone and all around not a great idea.

You can also set some process to run on another tty.  I'm almost certain this is what you actually want to do.  The exact procedure depends on the process you want to run.  So, what are you really trying to do?


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2016-05-31 23:17:54

mdodkins
Member
Registered: 2016-05-31
Posts: 5

Re: Executing commands on other ttys

Thanks for explaining all that, it's helped me understand better.

You could send a command string to the stdin of the bash shell already running in tty2.  This would required some exploration to find the pid of that bash session, and sending the string to a fd under /proc.  This is error-prone and all around not a great idea.

You can also set some process to run on another tty.  I'm almost certain this is what you actually want to do.  The exact procedure depends on the process you want to run.  So, what are you really trying to do?

Yes, either of these would be cool smile

I would like to use one terminal as though I'm sitting at the other, essentially. I'm using a serial tty for *my* input and don't have any direct route to the stdin on the bash running on the regular tty, although i can see the stdout on the device's screen. I'd like to have an interactive session where I can type commands and watch the output of anything i enter into bash via this session on the tty displayed on the screen.

Last edited by mdodkins (2016-05-31 23:23:22)

Offline

#5 2016-05-31 23:20:17

mdodkins
Member
Registered: 2016-05-31
Posts: 5

Re: Executing commands on other ttys

ls > /dev/tty2

That's cool! Not quite what I wanted, but cool!

Offline

#6 2016-05-31 23:22:04

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,534
Website

Re: Executing commands on other ttys

Ah, that's easy.  The best way by far is to use screen or tmux.  Both ttys can connect to the same tmux session.  In a way it's like a "google docs" tty session as both seats can enter commands and see the results.  This can actually be very odd if there is a person in each seat.  In this case, you can also specify that one of the connections is readonly, so that connection can see the commands being typed and the results of them, but can't interfer with the entering of the commands.

In the controlling terminal:

tmux new-session -s mysessionname

In the read-only terminal:

tmux attach -rt mysessionname

Now all commands typed into the controlling terminal are visible in the read-only terminal as is the output of those commands.  To have both terminals allowed to edit commands, remove the 'r' from the second command.  "mysessionname" is just an arbitrary name, you can call it something else as desired.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2016-05-31 23:27:29

mdodkins
Member
Registered: 2016-05-31
Posts: 5

Re: Executing commands on other ttys

I had a go with screen, but I think I must have been doing something wrong...

I tried

screen /dev/ttyN

... and although I can type commands in, pressing enter doesn't do anything. Is there something simple I'm doing wrong? I read the screen man pages and tried a few variations of "screen -X" but didn't get anywhere at all with it!

I think I'm missing something, but I'm not sure what....

Offline

#8 2016-05-31 23:29:13

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,534
Website

Re: Executing commands on other ttys

See my edit above.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#9 2016-06-01 00:17:03

mdodkins
Member
Registered: 2016-05-31
Posts: 5

Re: Executing commands on other ttys

Thanks! Very helpful, much appreciated smile

Offline

Board footer

Powered by FluxBB