How Linux Screen Command Works

Published on Author bloggereightLeave a comment

Program/Process will not disconnect if we run the program using the Screen command.

Screen command offers that ability to detach a long running process (or program, or shell-script) from a session and then attach it back at a later time.

When the session is detached, the process that was originally started from the screen is still running and managed by the screen. You can then re-attach the session at a later time, and your terminals are still there, the way you left them.

Method#1: Execute a command (or shell-script), and detach the screen

For example if you execute a tail or watch command or shell-script as shown below from the command.
# tail -f /var/log/messages
# ./test-script.sh
Instead of we can use the screen command to avoid the seesion disconnection, examples are mentioned below.
# screen tail -f /var/log/messages
# screen ./test-script.sh

Can see the list of screen processes.

# screen -ls
There is a screen on:
	4491.pts-2.FC547	(Attached)
1 Socket in /var/run/screen/rs-test.

Once we used the screen command, we can detach it from the onether terminal using any one of the following method If the session is diconnected, after login into new putty.

We will get into a new window. The title will be [screen0:screen] , now we can work on this if want a new sub screen, Ctrl+a c (this can be done many times)

Now the title will change into [screen 1: Screentest] [screen 2: Screentest]

To toggle these numbered screens uses.
Ctrl+a 0-9 -- Go to a window numbered 0 9, use Ctrl+a w to see number

Method#2: Detach the screen using CTRL+A d

When the command is executing, press CTRL+A followed by d to detach the screen.

Screen Detach using -d option

You can also detach it from terminal 1 to terminal 2 as shown below.
# screen -d SCREENID

# screen -d 4491.pts-2.FC547
[4491.pts-2.FC547 detached.]

Method#3: Attach the Screen when required

You can attach the screen at anytime by specifying the screen id as shown below. You can get the screen id from the “screen -ls” command output.
# screen -r 4491.pts-2.FC547

Method#4: Screen Share

#screen -x SCREENID
To detach and logout the screen, use just Ctrl+d
When you are working in a helpdesk, you might have challenges to get few things clarified.
1. Screen command will use if some script will take time to complete, so we dont want to wait upto the script complete.
2. we can even start some process from their machine using screen command and detach it when you are done. Later when you get back to your desk, you can login and attach the screen back to your terminal.

Comments

comments

Leave a Reply

Your email address will not be published. Required fields are marked *