Using screen as your login shell

GNU screen is a nice utility that allows running multiple interactive shells from the same terminal session and allows you to detach from your terminal while keeping those shells alive. Later on, you can re-attach to your background screen to get back to your shells. It has a lot more features like automatic session logging and terminal window splitting. You can discover them all in the manual.

How many times did you start a long-running task like gcc compilation on a remote server and then suddenly needed to disconnect from your shell? Maybe you just needed to move to some other place with your laptop, but if you disconnected from your LAN, your ssh connection would go down. How many times you thought “Damn, if I had launched screen before this…”?

The trick to save your compile time and not break your schedule is simple: just have your shell .profile script run screen at startup on your remote server. For bash, the syntax is simple, just add the following line at the end of your ˜/.profile script:

if [ ${SHLVL} -eq 1 ]; then
    ((SHLVL+=1)); export SHLVL
    exec screen -R -e "^Ee" ${SHELL} -l
fi

Quick implementation notes:

  1. Parameter -R reattaches to an existing detached session, if it exists, otherwise creates a new one.
  2. Parameter -e sets a non-standard escape character. This is useful since you don’t want login screen to interfere with other screens you may spawn during your activity. I chose Ctrl-E as it’s not used by other well-known keyboard shortcuts and works on most OSes.

To detach from your server type Ctrl-E d or just close your terminal window. Running processes will remain active in background, without detaching from your shell. When you connect to your remote shell again, you’ll get back to your session.

Do you like Unix tips like this? Follow me on Twitter or subscribe to my RSS feed for more.

This entry was posted in Sysadmin, Tips and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

6 Comments

  1. Posted February 10, 2010 at 8:25 PM | Permalink

    Fantastic! You've changed how I login to my server forever!

  2. blueben
    Posted February 11, 2010 at 12:46 AM | Permalink

    A catch: If you use SSH ForwardAgent, you will lose your connection to the agent from within a screen session when you drop and later reconnect. It's obvious in hindsight, but frustrating before you figure it out.

  3. Posted February 11, 2010 at 1:44 AM | Permalink

    @Nick: I'm happy you enjoyed :-)

    @blueben: yep, that's true. I've found a tip on how to solve this problem here: http://alan.lamielle.net/2009/03/09/environment…

  4. blueben
    Posted February 11, 2010 at 1:51 AM | Permalink

    Now if only I used zsh :)

  5. Posted February 11, 2010 at 1:53 AM | Permalink

    C'mon, treat it as pseudo-code :D

  6. qmega
    Posted August 3, 2010 at 5:03 AM | Permalink

    The only problem with this is that if screen fails for some reason you're pretty much screwed. Another option is to add

    screen -R && exit

    or something similar to the end of your .profile, so that if screen fails to start for some reason (wrong perms on screen dir or something) you stay in your bash shell and you can fix it.

2 Trackbacks

  1. [...] Using screen as your login shell [...]

  2. By links for 2010-04-12 « /home/servrrockr on April 13, 2010 at 12:54 AM

    [...] Using screen as your login shell — geek scrap (tags: screen shell) [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>