Easy to understand : How to efficiently use screen command and multiplex your terminal session ?

1. Introduction to Screen
Sometimes when we take remote console session and want to have multiple terminal sessions, however the login process is quite long.

Then we can use this interesting command to split our terminal session and then work on the other window. There is no need to login on new session.

Screen works like a terminal multiplexer. It helps us to open multiple sessions/screens of our terminal.

2. Advantage of using screen
i. The command/task running within screen on a remote machine used to run in background, even when the connection is lost/terminated.
ii. It allows us to resume our sessions.
iii. There is no need to login again on remote server, it multiplexes the current terminal session.

3. Using Screen Command

a. Run screen command twice.

### Check if the screen command is installed
ngelinux:~ saket$ screen --version
Screen version 4.00.03 (FAU) 23-Oct-06

### Run screen command
### Come out by pressing Ctrl+d
ngelinux:~ saket$ screen
bash-3.2$ 

### Run screen command again and then top command on terminal
ngelinux:~ saket$ screen
bash-3.2$ top
Processes: 391 total, 4 running, 387 sleeping, 1603 threads                                                                               00:43:39
Load Avg: 1.83, 1.86, 1.79  CPU usage: 7.67% user, 15.34% sys, 76.97% idle   SharedLibs: 208M resident, 47M data, 59M linkedit.
MemRegions: 81093 total, 2553M resident, 67M private, 720M shared. PhysMem: 8109M used (1791M wired), 82M unused.
VM: 1716G vsize, 1089M framework vsize, 3646(0) swapins, 4414(0) swapouts. Networks: packets: 14740498/18G in, 4783755/622M out.
Disks: 772104/15G read, 772984/24G written.

PID   COMMAND      %CPU TIME     #TH   #WQ  #PORTS MEM    PURG   CMPRS  PGRP PPID STATE    BOOSTS          %CPU_ME %CPU_OTHRS UID  FAULTS   COW
5390  top          3.1  00:00.47 1/1   0    23     5704K  0B     0B     5390 5344 running  *0[1]           0.00000 0.00000    0    4165+    104 
5381  auditd       0.0  00:00.01 3     2    31-    644K-  0B     0B     5381 1    sleeping *0[1]           0.00000 0.00000    0    715+     200 
5344  bash         0.0  00:00.00 1     0    19     636K   0B     0B     5344 5343 sleeping *0[1]           0.00000 0.00000    505  633      177 
5343  login        0.0  00:00.03 2     1    30     1100K  0B     0B     5343 5342 sleeping *0[9]           0.00000 0.00000    0    1578     178 

### Detach your screen session by pressing Ctrl+d

b. Check out logged screen sessions and attach to the sessions again.

### Check available sessions using "screen -ls" command.
ngelinux:~ saket$ screen -ls
There are screens on:
	4928.ttys000.ngelinux	(Detached)
	5025.ttys000.ngelinux	(Detached)
2 Sockets in /var/folders/1g/08s1b_9s57sfkn8dtx3h2k7r0000gs/T/.screen.
[pid.]tty.host

### re-attach to the detached session using "-r" option.
ngelinux:~ saket$ screen -r 4928.ttys000.ngelinux
[detached]
ngelinux:~ saket$ Ctrl+a and then press d

When we re-attach then we will see the top command output.

4. Multiplexing your current session: Split Windows and create new session

1. Press Ctrl+a and then “Capital/Uppercase S(or Shift+s)” to create a new session.
### The new session is not yet started, and is dead as of now.
2. Now press Ctrl+a and then TAB to goto newly created split window.
3. Press Ctrl+a and then “c”(lowercase c, i.e. without shift key) to start the session.

You will find the window like below:


You can press Ctrl+A, and then “X” to close the pane.

Leave a Reply

avatar
  Subscribe  
Notify of