This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature

Take console productivity to a new level with Screen

By Dean Henrichsmeyer on November 30, 2004 (8:00:00 AM)

Share    Print    Comments   

Screen is an application that's often underestimated. Screen is, simply put, a screen manager with VT100/ANSI terminal emulation. Think of it as a full-screen, text-based window manager for your terminal or console. For what it is, it's an incredibly feature-rich application. In this article, I will explain what it does and why it's so useful.
According to the man page, Screen "multiplexes a physical terminal between several processes (typically interactive shells)." What this means to the rest of us is that you start up Screen and run something in it (typically with a shell), and do whatever you normally would in a terminal. This is all fine and dandy but in and of itself isn't anything new.

Perhaps the most popular feature of Screen is something called detaching. Let's start with an example. It's 4 p.m. and you need to start compiling an application that takes around three hours to compile. After it compiles, you need to reboot the system. All this needs to be done by tomorrow morning. Typically that would mean spending the evening at the office, but not with Screen. With Screen you can start it up, start compiling the application, and then detach from the screen session altogether. Detaching will take you back to the command line from which you originally ran screen. At that point you can actually log out of the system and go home for dinner.

Before you panic, let me remind you that you did not suspend the compile job or forfeit your rights to the processor. It's still compiling. You just don't have to keep that specific terminal open in order to keep tabs on it. After you've enjoyed a nice dinner at home, you open up your laptop and SSH (Secure Shell) into the system, re-attach your Screen session and see what happened with the compilation. You'll be at the same command prompt where you started the compilation, complete with a buffer of what happened. You then simply reboot the system remotely and you're ready for the meeting in the morning -- and it didn't take all evening.

To start Screen, type: screen. That starts Screen and runs an instance of your interactive shell (usually Bash). You can do whatever you want in there, just as if it was just another one of your xterms. By default, each command to Screen begins with a C-a (Control-a), and is followed by at least one other keystroke. For example, when you're ready to detach the session you hit C-a, followed by d. When you decide to re-attach the session, you will type something like screen -r with optional arguments including the sessionowner, pid, tty, and/or host.

Now that you're convinced that Screen is useful, let's talk about being productive with it. The Screen feature that as been the most useful to me is the windowing capability. Screen allows you to have multiple windows in one session. You can have one window that's compiling something on one machine, another that's SSHed into another server editing a configuration file, and yet another monitoring a log server. Already do this with just a group of xterms on your screen? Let's go through an example where Screen really shines.

My name is Joe LinuxAdmin and I administer a cluster of six Linux servers doing various tasks, ranging from Web serving to file and printer sharing. What I need is a portable system administration environment. I've decided to use Screen. I start my session by typing screen. I now have something that I can remotely detach and re-attach, so that's good. I'd like to have an open shell into each of the servers I administer, so I'll create a window for each one and log in to each server. (To create a new window, I type C-a followed by c.) I can do this as many times as I want, so it's easy to create the six windows I'll need to access all of my servers at once.

After I've created my windows, I need to navigate them. To go to window number four, I'll type C-a 4. Instantly I'm viewing window number four. If I want to switch to the next window, I type C-a n. Now that I've got a server with the hostname of zeus on window 5, how am I going to remember that zeus is on 5? I can always hit C-a A and type in zeus. After I name my windows, I don't need to cycle through them. I can just as easily type C-a ', then type in the name of the machine I want -- and there it is. If I'm especially conservative with my keystrokes, then C-a " will show me a list of my windows along with their titles.

I now have, in one terminal, windows open to all of the servers I administer. Everything is right at my fingertips. I've got it all set up. It's now 5 p.m. and time to go home. Around 8 p.m. I'm out for a walk with my date. Lo and behold, my pager goes off. As usual I look for the nearest computer to address the situation and end up in a cybercafe on a Windows machine. Having a free SSH client on the windows machine is easy thanks to PuTTY, but it's going to take me forever to load up all of those terminals and login to all of the machines. It's too bad I left my Screen session attached at the office.

But wait! This is where Screen excels. I log in to my machine and type screen -dr. It remotely detaches my session and re-attaches it where I am. I now have my Screen session with all of my windows, all of my servers, and my entire administration environment right in the cafe. None of it is dependent on the client I'm using. I could be on a Linux machine, a Windows machine, or a Mac OS X machine.

Having windows for different servers you're logged into is just one example of the uses for multiple windows within Screen. It works equally well for a development environment. You can have one window for a text editor, another for compiling, another for debugging, and yet another running your application. It's a mobile environment that can be accessed from anywhere.

Screen has many powerful features that go beyond attaching and detaching. For example, you can have more than one person at a time attached to a screen session. Real-time editing by two people on one file -- how's that for collaboration? You can also password-protected Screen sessions to add a level of security. In addition, you can have more than one Screen session per user. The possibilities are endless.

Next time you're looking for a level of flexibility at the command line that you feel just isn't possible, I recommend reading up on Screen. It's a powerful utility that will enhance your productivity -- and at the same time it will make your life easier.

Share    Print    Comments   

Comments

on Take console productivity to a new level with Screen

Note: Comments are owned by the poster. We are not responsible for their content.

Re:C-a c

Posted by: Anonymous Coward on December 01, 2004 06:09 AM
i have been using Screen for more than a year now. it works great for me. except i would want to change the default C-a to some other combination like C-M-a or some other key combines. i haven't seen to do this through the resource files. is there any way to accomplish this?

#

more tidbits

Posted by: Anonymous Coward on December 03, 2004 12:25 AM
some more useful things about screen.

Ca-x will lock your screen session.

screen -D -r will force detach the existing session, and reattach it at your current terminal.

screen -l will list current sessions.

screen -x will attach to that session without detaching the other session. you can be logged into the same session twice, and even use the screen terminals independantly.

and as an update to the screenrc posted here, with that status line, you can add:
activity "%C -> %n%f %t activity!"
bell "%C -> %n%f %t bell!~"

that will highlight terminals in the status bar that have activity. I run naim in screen, and if someone chats me up, I can see the alert.

#

relief joint

Posted by: Anonymous Coward on May 28, 2006 07:16 PM
<tt>[URL=http://painrelief.fanspace.com/index.htm] Pain relief [/URL]
[URL=http://lowerbackpain.0pi.com/backpain.htm] Back Pain [/URL]
[URL=http://painreliefproduct.guildspace.com] Pain relief [/URL]
[URL=http://painreliefmedic.friendpages.c<nobr>o<wbr></nobr> m] Pain relief [/URL]
[URL=http://nervepainrelief.jeeran.com/pa<nobr>i<wbr></nobr> nrelief.htm] Nerve pain relief [/URL]</tt>

#

Copy &amp; Paste

Posted by: Anonymous Coward on December 03, 2004 05:35 PM
You can also copy and paste between screens using 'C-a [' to move to the start of a block, press enter, highlight the block with cursor keys, and press enter again.

To paste simply move to the screen you want and use 'C-a ]'

Fantastic.

#

for sysadmins only

Posted by: Anonymous Coward on December 03, 2004 11:08 PM
here's what i get out of the article: sysadmins love screen. if you're not a sysadmin, become one and try screen. oh, you're not a sysadmin? don't waste your time here. screen's not for you. maybe that should be on the opening line? or maybe there should be at least an attempt to write about the advantages of screen for the rest of the world.

#

Re:for sysadmins only

Posted by: Anonymous Coward on December 04, 2004 03:00 AM
oh, you're not a sysadmin? don't waste your time here. screen's not for you.


Where did you discover this?

#

lower back pain

Posted by: Anonymous Coward on May 28, 2006 07:06 PM
[URL=http://painrelief.fanspace.com/index.htm] Pain relief [/URL]

  [URL=http://lowerbackpain.0pi.com/backpain.htm] Back Pain [/URL]

  [URL=http://painreliefproduct.guildspace.com] Pain relief [/URL]
[URL=http://painreliefmedic.friendpages.com] Pain relief [/URL]
[URL=http://nervepainrelief.jeeran.com/painrelief<nobr>.<wbr></nobr> htm] Nerve pain relief [/URL]

#

one screen per server

Posted by: Anonymous Coward on December 04, 2004 01:07 AM
I have an OpenBSD box in both our campuses, each of which has one screen session per server that I admin. I name each session when I create it, and tell it to automatically SSH into that server when I create a new window:
screen -S servername -s servername.ssh
where servername.ssh is just a script to SSH into that server.
This way, I can run screen --list and see which servers I have active sessions on, and then just
screen -r servername to reconnect.

#

Re:C-a c

Posted by: Dean Henrichsmeyer on December 01, 2004 09:22 AM
Right, missed that in the edit, thanks, it's fixed.

#

.screenrc

Posted by: Administrator on December 01, 2004 09:29 AM
Stolen from various sources, the status line is the most useful:
<TT># Prepend/append register [/] to the paste if ^a^] is pressed.
# This lets me have autoindent modes in vi(m).
register [ "\033:se paste\015a"
register ] "\033:se nopaste\015a"
bind ^] paste [.]

 
#kill startup message
startup_message off

 
# define a bigger scrollback, default is 100 lines
defscrollback 1024

 
# on Debian backspace should send char `177':
# bindkey -k kb stuff "177"
bindkey -k kb stuff ^H
#It displays a status line at the bottom of the terminal which shows what window you're in and a list of the other windows.
termcapinfo xterm "ks=E[?1lE:kuE[A:kd=E[B:kl=E[D:kr=E[C:kh=E[5~:kH=<nobr>E<wbr></nobr> [F"
hardstatus alwayslastline "%{-b gk}%-w%{+b kg}%50>%n %t%{-b gk}%+w%<"

 
#default windows
screen -t vim 1 vim
screen -t mail 2 pine
screen -t web 3 elinks</TT>

#

C-a c

Posted by: Administrator on November 30, 2004 06:53 PM
Minor note: C-a c creates a new window, not C-a n. Nice article otherwise.

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya