Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
TIP: Let GNU/screen know where to open X apps!
View unanswered posts
View posts from last 24 hours

Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message

Joined: 26 Sep 2004
Posts: 1386
Location: NC, USA

PostPosted: Mon Jan 09, 2006 7:32 pm    Post subject: TIP: Let GNU/screen know where to open X apps! Reply with quote

GNU/screen is an amazing application which allows you to open many shells and terminal applications within a single login session, but more importantly, it allows you to separate a terminal session from any particular login instance. Hence, you can start something long (like an emerge process) at home, then check on that same process later from work, via SSH.

emerge screen

I won't give a tutorial of screen here -- those are easily found elsewhere by googling for "GNU screen".

For those of us who live or die by GNU/screen, one annoyance is that when you start a screen session on machine A and then attatch to that screen session from machine B, any X applications (firefox) will still open on machine A. Of course, this makes sense, because screen lives above the login instance, and it doesn't inherit the new environment info(specifically, the $DISPLAY variable) for already-running processes.

Here is a quick modification you can make to have the new X applications open on machine B.

Note that the "proper" way to abstract an X session is with VNC, but I've found VNC to be too heavy when all I really want is a constant terminal history following me wherever I go. With this, if I launch a simple X app, it follows the behavior I'd like to expect: the window comes up where I am instead of just barfing or appearing on some far-off machine.

  • First, whenever you log in remotely, a simple file ($HOME/.Xscreen) is updated with the new $DISPLAY. This also (re)attaches to any existing screen.:

    $HOME/.bash_profile snippet

        if [ "${TERM}" != "screen" ]; then
              [ -n "${DISPLAY}" ] && echo "${DISPLAY}" > "${HOME}/.Xscreen"
              screen -xR
              echo "I don't want to run screen within screen!  (~/.bash_profile)"

    The outer "if; then; ... fi" is just to make sure we don't get nested screens, because that becomes reminiscant of Being John Malkovich.
    I also wrap this in a "if $(hostname) == myfavoritehost; then ... fi" test, since I only have a persistent screen session on one machine.

  • Now, we want screen to tell its subshells to check the $HOME/.Xscreen file for updates:

    $HOME/.screenrc snippet

    setenv PROMPT_COMMAND 'DISPLAY="$(cat ~/.Xscreen)"'

    That is, whenever a new prompt comes up, the shell session will check that file for the new X display.

    Be sure that this is BEFORE any commands like "screen 1 COMMAND" in your .screenrc, or those COMMAND's won't get this command.

Now, test:

1) Log into machine A locally
2) A: kill any existing screen sessions there and do 'screen -xR' (to get the new config)
3) A: in one of the screen windows, type "xclock". You should see the xclock on machine A.
4) Log into machine B. From machine B, ssh -X machineA. It should go into the screen session already created.
5) B: hit "enter" a couple times so PROMPT_COMMAND runs.
6) B: 'xclock' again should produce an xclock on machine B.

Question for the readers: Faster/better ways to do this than cat'ing a file?
Back to top
View user's profile Send private message

Joined: 11 Jan 2005
Posts: 360

PostPosted: Tue Jan 10, 2006 12:32 am    Post subject: Reply with quote

Great! I've been looking for a solution to this for a while, thanks!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum