Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

So lets say you boot up your linux install all the way to the desktop. You start up a gnome-terminal/konsole/whatever so you have a tty to enter commands to.

Now lets say I SSH into that same machine. It will bind me to another tty to enter commands to.

Now lets say I want to "switch" my tty from my original SSH one to the gnome-terminal one started earlier.

Basically I'm asking if there is anyway to do the same thing screen -x does but without screen?

I know you can easily send output to the other tty simply by echoing something into the /dev file, but I dont know a way to 'view' whats in the tty...

Any ideas?

share|improve this question
    
    
Is this a duplicate of unix.stackexchange.com/questions/8469/… ? –  mattdm Aug 1 '11 at 20:44

2 Answers 2

up vote 9 down vote accepted

Maybe these schema can clarify the situation. This is the usual setting:

                               Terminal  (/dev/ttyX or /dev/pts/x)
                                device
                                   |
                    (screen)<--[<output]----x-------(stdout) Process1
        Terminal  (keyboard)---[input >]---o-\----->(stdin)
                                            \ \
(hardware console or                         \ `----(stdout) Process2
 virtual console or terminal                  `---->(stdin)
 emulators like xterm, …)

And there is no way to plug some new Process3 like this:

                             Terminal
                              device
                                 |
             (screen)<---o---[<output]--x------(stdout) Process1
  Terminal (keyboard)---/-x--[input >]-o-\---->(stdin)
                       | /              \ \
                       | |               \ `---(stdout) Process2
                       | |                `--->(stdin)
                       | |
                       \ `---------------------(stdout) Process3
                        `--------------------->(stdin)

What screen (and others) does is allocating some pseudo terminal device (like xterm does) and redirect it to one or more "real" terminals (physical, virtual, or emulated):

             Terminal                   pseudo
             devices              ,--> Terminal (/dev/pts/x)
                |         _______/      device
Terminal <--[<output]--- |       |        |
 1       ---[input >]--> |screen | <--[<output]---x-----(stdout) Process1
                         |Process| ---[input >]--o-\--->(stdin)
Terminal <--[<output]--- |       |                \ \
 2       ---[input >]--> |_______|                 \ `--(stdout) Process2
                                                    `-->(stdin)

Using screen -x you can attach one more terminal, xterm, whatever (say Terminal 3) to the screen session.

Hope this helps!

share|improve this answer
    
That's a great explanation! I think it's not really an answer here, but would suit this question well. One observation: “there is no way” is only true if you forbid ptrace; thanks to ptrace, programs like neercs, retty and so on can do it sometimes. –  Gilles Aug 2 '11 at 22:43
    
@Gilles: well they do something else: they change the value of the file descriptors (stdin,stdout on the right of the schema) directly by hijacking the process. Pure evil! –  Stéphane Gimenez Aug 2 '11 at 22:47
1  
Hmm, you're right, they change a different arrow. Evil, but convenient! –  Gilles Aug 2 '11 at 22:49

Reconnecting the processes on the other terminal to your current terminal is not possible without dirty tricks. It is possible by forcing the process to perform certain system calls (with ptrace); this causes some programs to crash. There are several tools that do this, such as neercs, retty, cryopid, reptyr, …; see How can I disown it a running process and associate it to a new screen shell? and linked questions.

Obtaining the output already displayed on the other terminal is a different problem. There is no fully general solution: in principle, once the output has been rendered, the terminal might store it only as an image. In practice, all X terminal emulators keep their output buffer in text form so that you can copy-paste it. The way to grab that output depends on the terminal emulator; the basic idea is to simulate selecting the whole scrollback buffer. For a Linux console such as /dev/tty1, the scrollback buffer is easily available as /dev/vcs1 (and /dev/vcsa1 with text attributes).

The simple answer is that if you want to reconnect to a terminal from a different place, use screen or tmux.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.