Search This Blog

Saturday, August 16, 2014

Unix Command: screen

I guess you all know this: you are connected to your server with SSH and in the middle of compiling some software (e.g. a new kernel) or doing some other task, which takes lots of time, and suddenly your connection drops for some reason, and you lose your labour. This can be very annoying, but fortunately there is a small utility called screen which lets you reattach to a previous session so that you can finish your task. This short tutorial shows how to use screen for just this purpose.

Benefits of Screen Command:

1)  Remote terminal session management (detaching or sharing terminal sessions)
2)  unlimited windows
3)  scroll back buffer
4)  copy/paste between windows
5)  notification of either activity or inactivity in a window
6)  split terminal (horizontally and vertically) into multiple regions
7)  locking other users out of terminal


Starting Screen
Screen is started from the command line just like any other command:

[root@office ~]# screen
You are now inside of a window within screen. This functions just like a normal shell except for a few special characters.

Command: “Ctrl-a”
Screen uses the command “Ctrl-a” that is the control key and a lowercase “a” as a signal to send commands to screen instead of the shell.
For example, “Ctrl-a” then “?”. You should now have the screen help page.

  Screen key bindings, page 1 of 1.

                         Command key:  ^A   Literal ^A:  a

break       ^B b       fit         F          lastmsg     ^M m       number      N          redisplay   ^L l       suspend     ^Z z       writebuf    >        
clear       C          flow        ^F f       license     ,          only        Q          remove      X          time        ^T t       xoff        ^S s     
colon       :          focus       ^I         lockscreen  ^X x       other       ^A         removebuf   =          title       A          xon         ^Q q     
copy        ^[ [       hardcopy    h          log         H          pow_break   B          reset       Z          vbell       ^G       
detach      ^D d       help        ?          login       L          pow_detach  D          screen      ^C c       version     v        
digraph     ^V         history     { }        meta        a          prev        ^H ^P p ^? select      '          width       W        
displays    *          info        i          monitor     M          quit        \          silence     _          windows     ^W w     
dumptermcap .          kill        K k        next        ^@ ^N sp n readbuf     <          split       S          wrap        ^R r     

^]  paste .
"   windowlist -b
-   select -
0   select 0
1   select 1
2   select 2
3   select 3
4   select 4
5   select 5
6   select 6
7   select 7
8   select 8
9   select 9
I   login on
O   login off
]   paste .


Key bindings are the commands the screen accepts after you hit “Ctrl-a”. You can reconfigure these keys to your liking using a .screenrc file, but I just use the defaults.

Creating Windows

Command: “Ctrl-a” “c”.
To create a new window, you just use “Ctrl-a” “c”.
This will create a new window for you with your default prompt.  Your old window is still active.
For example, I can be running “top” or “sleep 1000” and then open a new window to do other things. Top stays running! It is still there. To try this for yourself, start-up screen and then run top. (Note: I have truncated some screens to save space.)
Start top
Load averages: 0.04, 0.04, 0.05
273 processes: 203 sleeping, 70 running
Cpu states:
CPU   LOAD   USER   NICE    SYS   IDLE  BLOCK  SWAIT   INTR   SSYS
 0    0.04   0.4%   0.0%   0.2%  99.4%   0.0%   0.0%   0.0%   0.0%
 1    0.03   0.0%   0.0%   1.2%  98.8%   0.0%   0.0%   0.0%   0.0%
 2    0.05   0.0%   0.0%   0.0% 100.0%   0.0%   0.0%   0.0%   0.0%
 3    0.03   0.6%   0.0%   2.7%  96.7%   0.0%   0.0%   0.0%   0.0%
---   ----  -----  -----  -----  -----  -----  -----  -----  -----
avg   0.04   0.2%   0.0%   1.0%  98.8%   0.0%   0.0%   0.0%   0.0%

Now open a new window with "Ctrl-a” “c”
Your top window is still running you just have to switch back to it.

Switching Between Windows

Command: “Ctrl-a” “n”
Screen allows you to move forward and back. In the example above, you could use “Ctrl-a “n” to get back to top. This command switches you to the next window.
The windows work like a roundabout and will loop back around to your first window.
You can create several windows and toggle through them with “Ctrl-a” “n” for the next window or “Ctrl-a” “p” for the previous window.
Each process will keep running until you kill that window.
You could also use:
# “Ctrl-a” “Ctrl-a”   # Toggle / switch between the current and previous window
# “Ctrl-a” “0-9”      #Go to a window numbered 0 9, “Ctrl-a” “w” to see number
# “Ctrl-a” “Spacebar” #Go to next Terminal
# “Ctrl-a” “Backspace”      #Come back to previous terminal

 

 

Detaching From Screen

Command: "Ctrl-a" "d"
Detaching is the most powerful part of screen.  Screen allows you to detach from a window and reattach later.
If your network connection fails, screen will automatically detach your session!
You can detach from the window using “Ctrl-a” “d”.
This will drop you into your shell.
All screen windows are still there and you can re-attach to them later.
This is great when you are using rsync for server migration or during something, which you forget to keep in nohup.

bash-4.1$
[detached]

Reattach to Screen

If your connection drops or you have detached from a screen, you can re-attach by just running:

[jeffh@office ~]$ screen -r



This will re-attach to your screen.
However, if you have multiple screens you may get this:

ibhagat@office#screen -r
There are several suitable screens on:
        16076.pts-26.office   (Attached)
        2917.pts-142.office   (Detached)
        16873.pts-16.office   (Detached)
        4945.pts-13.office    (Attached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.

If you get this, just specify the screen you want.

[jeffh@office ~]$ screen -r  16873.pts-16.office

Logging Your Screen Output

I find it important to keep track of what I do to someone’s server. Fortunately, screen makes this easy.
Using “Ctrl-a” “H”, creates a running log of the session.
A file named “screenlog.0” will be created in your home directory.
Screen will keep appending data to the file through multiple sessions. Using the log function is very useful for capturing what you have done, especially if you are making many changes. If something goes awry, you can look back through your logs.

Getting Alerts

Screen can monitor a window for activity or inactivity. This is great if you are downloading large files, compiling, or waiting for output.
If you are waiting for output from a long running program, you can use “Ctrl-a” “M” to look for activity. Screen will then flash an alert at the bottom of the page when output is registered on that screen.

I use this when running a command that takes a long time to return data. I can just fire up the command, switch to another window and not have to keep switching back to check the status.

You can also monitor for inactivity. Why use this?

If you are downloading a large file or compiling a program, you can be notified when there is no more output. This is a great signal to when that job is done. To monitor for silence or no output use “Ctrl-A” “_”.

Locking Your Screen Session

If you need to step away from your computer for a minute, you can lock your screen session using "Ctrl-a" "x".  This will require a password to access the session again.

bash-4.1$            
Screen used by Indrajit Bhagat <ibhagat>.
Password:

Stopping Screen

When you are done with your work, I recommend you stop the session instead of saving it for later.  To stop screen you can usually just type exit from your shell. This will close that screen window.  You have to close all screen windows to terminate the session.
You should get a message about screen being terminated once you close all windows.

[screen is terminating]

Alternatively, you can use "Ctrl-a" "k".  You should get a message if you want to kill the screen.

Quit Screen:

Used “Ctrl-a Ctrl+\”

Setting the title of the Screen:
To start a new screen with a specific name, you could provide the name as below:

# screen –t foo
Num Name                                      Flags

  0 bash                                      $
  1 bash                                      $
  2 bash                                      $
  3 foo                                       $

To rename or to give name to existing screen, use the below command:

# “Ctrl-a :title foo”

Listing Screen Window:
# “Ctrl-a” “w”  #List all windows (the current window is marked with "*")

0$bash  1$ bash  2-$ bash  3*$ foo

Also
#”Ctrl+a” “ ” ” #List all window


Num Name                                      Flags

  0 bash                                      $
  1 bash                                      $
  2 bash                                      $
  3 foo                                       $
Using the screen as terminal multiplexer
# “Ctrl-a” “i”  #To Split the svreen vertically
# “Ctrl-a” “S”   #To Split the screen horizontally
# “Ctrl-a” “Q”   #To unsplit
Note: After splitting, you need to go into the new region and start a new session via ctrla then cbefore you can use that area.

To copy a block

To get into copy mode, press Ctrl-a [ .
To move the cursor, press the h, j, k, and l (the letter l) keys. The 0 (the number 0) or ^ (the caret) moves to the start of the line and $ (the dollar sign) moves to the end of the line. Ctrl-b scrolls the cursor back one page and Ctrl-f scrolls forward one page. To set the left and right margins of copy, press c and C (Shift-c). The Spacebar starts selecting the text and ends selecting the text. To abort copy mode, press Ctrl-g.

To paste a block

To paste the copied text to the current window (as many times as you want), press Ctrl-a ].

No comments:

Post a Comment