Saving session terminal states with screen command

Have you ever been using a terminal in Linux and mistakenly closed the terminal or found yourself disconnected from SSH right when you were working on something terminalimportant? You reopen the terminal and suddenly all your work has been lost or the script you were running stopped working half way through.

Screen is a Linux application which solves this problem by allowing you to save terminal session states each time you load a terminal using screen. Screen is a versatile application in that it allows you to run a virtually unlimited amount of terminal windows from a single terminal session. Each individual terminal screen you create using screen will store information in a buffer, so that you can scroll up and down the terminal history or copy and paste text between terminals.

 At first it might be a bit difficult for you to wrap your head around this concept, but it’s actually quite simple. Let’s take a more detailed look below.

 To find out if you have screen installed open up a terminal and type in any of the following commands:

 rpm –q screen
screen –help

 By default screen comes installed on most Linux distributions including Fedora, CentOS and Ubuntu.

 

To start a new screen session, the first thing you will need to do is just type in “screen”, once you hit enter, you probably wont notice much of a difference, but you have just created your first screen session. Below are a few commands which you can use to manage your screen sessions.

 To check what sessions screen has running type in the following command:

Screen –ls

 You will notice that the screen session that your currently using will display as (attached) and the sessions you have stored which you arnt used will be displayed as (detached).

 To enter into screens management controls you will need to press ctrl+a followed by the correct key for the action you want to take. Below are a few of the main keys you will need to memorize in order to use screen.

 

c = New window

k = Destroy window. If there are no other windows open it will destroy the session.

n = Switch between windows.

[ = Scroll up terminal history using the arrow keys or page up, page down.

C = Clear window

d = Detach screen session and go back to the regular terminal

If at any point you get stuck or you’ve forgotten a key which you wanted to use, simply pressing ctrl + a and then pressing h will bring up a list of keys which are displayed below.

break ^B b

license ,

removebuf =

clear C

lockscreen ^X x

reset Z

colon :

log H

screen ^C c

copy ^[ [

login L

select ‘

detach ^D d

meta a

silence _

digraph ^V

monitor M

split S

displays *

next ^@ ^N sp n

suspend ^Z z

dumptermcap .

number N

time ^T t

fit F

only Q

title A

flow ^F f

other ^A

vbell ^G

focus ^I

pow_break B

version v

hardcopy h

pow_detach D

width W

help ?

prev ^H ^P p ^?

windows ^W w

history { }

quit

wrap ^R r

info i

readbuf <

writebuf >

kill K k

redisplay ^L l

xoff ^S s

lastmsg ^M m

remove X

xon ^Q q

Once you have a basic understanding of how screen works, try typing in a few commands or running a basic script and then close the terminal. To reopen it you simply type “screen –ls” to find your previous session name and “screen –r sessionname” to reattach the session. The script will still be running and none of the work has been lost.

{module user9-footer}