A few things to do with screen command and screen session in Linux

Today we’ll try to see a few things we can do with the screen command.
If you wish to have multiple bash terminals running in the same terminal window
without creating multiple terminal windows or terminal tabs,
screen is the command you would want to aim at.

So let’s see what options does screen command gives us:

screen --help

And, we get the following details about the screen command.

Currently running screen sessions

screen -ls

Attaching a Detached screen sessions

If a screen session was detached from a bash terminal window,
and you wish your current bash window, to attach the that screen session process,
you can just type the following.

screen -r <process_id.your_screen_session_name>

You could also use only the your_screen_session_name without the process_id if the multiple screen sessions are named differently, like this:

screen -r <your_screen_session_name>

Detaching & Attaching an attached screen sessions

If you were disconnected from a screen session somehow, and the screen session never got detached,
you can detach it from the bash terminal where it is attached to, & attach it to your current bash terminal window,
and start working with it.

screen -D -r <process_id.your_screen_session_name>

Kill screen session

To kill the whole screen session, type:

Ctrl-a \

To Kill a window in a screen session

Let’s start a screen session with total 3 windows created

screen -S Local_Server -t my_local_server_1

Type the following 2 times, to create 2 more screen windows:

Ctrl-a c

Now to kill a window in screen,  go to that window
Since you created 2 more screen window in the same bash terminal, by typing the above command 2 times.
You will now have total 3 windows listed, starting from the number 0 to 2, inside screen type:

Ctrl-a Shift-"

We can see 3 Windows like this

Press Enter to select the window with id 1.

Now fire this command to kill the window.

Ctrl-a k

Press y if asked for confirmation like, Really kill this window?

And your window id 1 should be gone from the list now.

To scroll up the window in screen

If a lot of output has been printed on your terminal while in a screen session,
and you would want to scroll up & see what got printed, you can type:

Ctrl-a Esc k

Ctrl-a: The escape character
Esc: The Escape key, to enter into scroll mode
k: To scroll up
Others you can use:
j: Scroll up
h: Move cursor towards left
l: Move cursor towards right
You could also use the arrow keys instead of, j,k,h,l.
Type Esc key once to exit scrolling.

Changing the escape character in screen from Ctrl-a to other

Open the ~/.screenrc file that you should be having in your home folder (/home/your_username/.screenrc), if you don’t then you can just create it.
Let’s change the escape character from Ctrl-a to Ctrl-z
Enter the following in your ~/.screenrc and save it.

escape ^zz

Now start screen & your escape key has been changed from Ctrl-a to Ctrl-z.

Author: Admin

Hello Friends, welcome to Hackerdays.com. I am the Admin of the site. I have worked as a software programmer for 7 years. I have started this site to learn as well as share whatever little knowledge i have gained over the period of time. If you wish to contact me, you can drop in an email to: admin[AT]hackerdays[DOT]com or you can also use the contact page. Thank you and I'll see you around.

Let us know your thoughts: