The Magic of `screen`

    Screen is an awesome little utility that, in my opinion, doesn't get enough use by web developers.

    Wikipedia tells us that "GNU Screen is a software application that can be used to multiplex several virtual consoles, allowing a user to access multiple separate terminal sessions inside a single terminal window or remote terminal session. It is useful for dealing with multiple programs from a command line interface, and for separating programs from the Unix shell that started the program".

    As anyone who's used SSH over a crummy connection knows, having your session disconnect while you're in the middle of doing something (editing a file in the only editor that matters, running a long-running script, etc.) kind of sucks: your task stops running when your SSH session is disconnected.

    Using screen on your remote server helps prevent that problem. As soon as you log in, just run screen and you'll get a new terminal. If you type exit, that screen session will terminate. Here's a basic primer on what you can do with screen:

    • screen: start a new screen session
    • screen -ls: list all screen session
    • screen -r or screen -r [session name]: Re-attach a screen session

    While you're in a screen session, you can type Ctrl+a d to detach from the session.

    So when you get disconnected from SSH and you're running a screen session, you can reconnect and run screen -ls and you'll get a list of all the currently running screen sessions. The screen session that you got disconnected from will be listed as attached because you didn't properly detach from that session. To reconnect to that session, you'll need to use screen -x to reattach (or, if you have multiple screen sessions that are currently detached, you'll need to use screen -x [session name]).

    Another cool thing that this lets you do:

    1. Have two people log in to a remote server using the same user account.
    2. User 1 starts a screen session.
    3. User 2 attaches to that screen session using screen -x [session name]

    It's like screen sharing for SSH, and it's a great solution for low-bandwidth connections. Note that both users attached to a common screen session can type things in the terminal, so tread carefully - it can be very easy to make a mistake with TWO people at the wheel.

    For more information, man screen.

    Comments

    Deryck's picture
     

    Deryck (not verified) says:

    Yeah I got very intimate with screen when I had to administer a new RHEL dedicated to run about 12 servers. Tried using daemons but I needed console output for them constantly for monitoring and this little command saved my life.

    As a developer though, I'm either in Linux already on my desktop or laptop running a local server and syncing with FTP to the host or...really nothing at all. GoDaddy $3 hosting doesn't really give much user access via SSH.

    Good post though. I like good apps being advertised when they deserve it.

    Anonymous's picture
     

    Anonymous (not verified) says:

    You could have a look at tmux which is a terminal multiplexer as screen but with all the features you want: splitting both vertical and horizontal in general much more flexible.

    Anonymous's picture
     

    Anonymous (not verified) says:

    how low bandwidth are you developing at?

    ao2's picture
     

    ao2 (not verified) says:

    Hi, I use an alias to launch 'screen' automatically when I connect to the remote system via ssh, something like:

    function ressh()
    {
    ssh -t $@ screen -D -R
    }

    kotnik's picture
     

    kotnik (not verified) says:

    Even better, after getting hooked to screen, consider switching to tmux. Much better client/server model, emacs or vi keys, multiple paste buffers, search in windows option, etc.

    willvincent's picture
     

    willvincent (not verified) says:

    byobu is a cool enhancement for screen. It gives you multiple 'windows', and a status bar so you can see what's in each window, and other system info.

    Capi Etheriel's picture
     

    Capi Etheriel (not verified) says:

    It'd be interesting if you mentioned the issues between screen and the only editor that matters. Since screen changes $TERM to screen, you will have to work around it in your .vimrc file.

    By the way, do you host your dotfiles somewhere? Mine are on http://github.com/barraponto/dotfiles

    wizonesolutions's picture
     

    wizonesolutions (not verified) says:

    I echo the tmux recommendations. It can also be configured to work with Vim and not mess up the F keys. I've even used tmux inside of tmux. is an annoying prefix...but it did work, haha.

    Add new comment

    Filtered HTML

    • Web page addresses and e-mail addresses turn into links automatically.
    • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
    • Lines and paragraphs break automatically.

    Plain text

    • No HTML tags allowed.
    • Web page addresses and e-mail addresses turn into links automatically.
    • Lines and paragraphs break automatically.
    By submitting this form, you accept the Mollom privacy policy.