According to the GNU screen web page,
screen is …a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. What that means is: it lets users multitask within a single console window. It provides a feature similar to the Windows Task Manager, but it has even more benefits.
It couldn't be much simpler to start
screen…just run the command at a console prompt.
At this point, my terminal is cleared and my cursor moves to the first line1). The title bar in my SSH application changes too–it normally shows the hostname I'm connected to, but now it says “screen” instead.
I'll start a
top session. This will occupy my console until I press <ctrl>-c.
top - 16:32:06 up 60 days, 21 min, 1 user, load average: 2.00, 2.00, 2.00 Tasks: 102 total, 2 running, 100 sleeping, 0 stopped, 0 zombie Cpu(s): 15.4% us, 26.2% sy, 0.0% ni, 56.4% id, 0.3% wa, 1.4% hi, 0.3% si Mem: 4914404k total, 4870756k used, 43648k free, 346636k buffers Swap: 8289532k total, 0k used, 8289532k free, 3751860k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 7538 root 20 0 197m 9900 5024 S 101.3 0.2 61150:27 asterisk 28858 oracle 25 0 849m 26m 24m R 99.3 0.6 21010:20 oracle 1 root 16 0 4772 516 428 S 0.0 0.0 0:04.75 init 2 root RT 0 0 0 0 S 0.0 0.0 0:02.10 migration/0 3 root 34 19 0 0 0 S 0.0 0.0 0:00.09 ksoftirqd/0 4 root RT 0 0 0 0 S 0.0 0.0 0:02.79 migration/1 5 root 34 19 0 0 0 S 0.0 0.0 0:00.23 ksoftirqd/1 6 root RT 0 0 0 0 S 0.0 0.0 0:01.22 migration/2 7 root 34 19 0 0 0 S 0.0 0.0 0:00.05 ksoftirqd/2 8 root RT 0 0 0 0 S 0.0 0.0 0:00.98 migration/3 9 root 34 19 0 0 0 S 0.0 0.0 0:00.02 ksoftirqd/3
Normally I would need to start a second ssh connection to interact with the machine while top is running, but here's where the magic of
<ctrl>-a c key combination (press and hold
<ctrl> while pressing
a, then release both and press
c). This will create a new virtual console and place it in front of the previous screen. It will appear to be a new console with a shell prompt.
The only visible difference is that the terminal title will change if your terminal setting allow it. My title changed to ”
[screen 1: bash]”. Now I can perform any tasks I need to as if the other console didn't exist. I can create up to 9 virtual consoles this way.
Having more than one virtual console is great, but you'll need to switch between them. That's easy:
<ctrl>-a 9brings console 0 thru 9 to the front, respectively
<ctrl>-a nbrings the 'next' console to the front
<ctrl>-a pbrings the 'previous' console to the front
<ctrl>-a ”displays a list of all consoles and select one using up/down/enter keys
This is a huge bonus for those of us who write software because we can (for example):
In other words, it helps to minimize the distractions and wasted time caused by changing directories, getting in/out of various programs like tail and top, etc.
screen session remains in memory until you exit all of its consoles manually. Suppose I lost connectivity to one of my machines 2). Under normal circumstances, I would lose every window that I had open on the machine in question. But thanks to
screen, that changes.
Every virtual console is stored inside my screen “session”, and that session stays in memory even if my connection is broken. So I can login again and find/reconnect to my session like this:
login as: xxxxxx xxxxxx@mymachine's password: Last login: Tue Jul 28 17:11:24 2009 from yyyyyyyy -bash-3.00# screen -ls There is a screen on: 19645.pts-0.mymachine (Detached)
Aha…my screen session is named
19645.pts-0.mymachine. So I'll reconnect to it.
bash-3.00# screen -r 19645.pts-0.mymachine
And I'm back right where I was before my VPN flaked out. No lost productivity. No need to reestablish all the SSH connections that were up and running. My connectivity and processes are all right where I left them.
screen even gives me a way to disconnect from my running active session:
<ctrl>-a d. I'll do this if I want to connect to another machine and run
screen over there (because otherwise my
<ctrl>-a keystrokes will be interpreted on my current machine).
You can navigate to any console and hit
<ctrl>-a A (note the capital A) and you will be allowed to enter a meaningful title for the window. This is useful when you have two of the same processes running and the process name doesn't tell you what the console's purpose is. When pressing the above key combination, the last line of the current terminal will display the following:
Set window's title to:
and you should enter a meaningful name and press enter. Below is an example with three consoles that have been given meaningful names:
Num Name Flags 0 Tail Window $ 1 Log Window $ 2 Interactive Window $