linux_wiki:screen

This is an old revision of the document!


Screen

Screen is a “full-screen window manager that multiplexes a physical terminal between several processes”. (screen man page)

This allows you to have multiple virtual terminal “workspaces” within 1 actual terminal session.

1) Install if it is not already:

yum install screen
or
apt-get install screen

2) Start a screen session

screen

Screen may or may not show some startup dialog, but once you are past that, it will look just like a normal terminal prompt.

3) To view a list of all key bindings:

Ctrl+a, then ?

The listed commands have multiple ways of being executed. You only have to press “Ctrl+a” and then the key letter for the desired action. Press Space ore Enter to leave the help screen.

Creating and Moving Between Screens

Start top in one screen, create a new screen, and cycle between the two.

Start top

top

Create a new screen

Ctrl+a, then c

You will be on the new screen session. Do something else to see the difference:

free -m

Now, you can cycle between the two screen sessions by doing this:

Ctrl+a, then n

You will notice that top was never interrupted, it was running the entire time on its screen session.

Detach and Reattach Screen

The real power of screen is detaching a session and connecting back to it later.

You can either intentionally detach or get a SSH/network interruption and reconnect to your still running screen session. Perfect for long running processes or scripts.

This can be demonstrated by executing a for loop that echos a number, sleeps for 1 second, and then keeps going.

1) If it is not open still, start screen

screen

2) Start a long for loop…this should take about 5 minutes to complete.

for NUM in {1..300}
do echo -e "Number is: ${NUM}"
echo "Sleeping for 1 second..."
sleep 1
done

3) Detach from screen after it has started.

Ctrl+a, then d

4) List screen processes for your user.

screen -ls

This will give you output similar to the following:

bill@dt-bill:~$ screen -ls
There is a screen on:
	6732.pts-1.dt-bill	(11/23/2014 07:28:40 PM)	(Detached)
1 Socket in /var/run/screen/S-bill.

bill@dt-bill:~$ ps -ef | grep 6732
bill      6732  2738  0 19:28 ?        00:00:00 SCREEN

You can see that the number is indeed a process ID.

5) Reattach to the screen:

screen -r 6732

Note: You only have to specify the screen PID if you have multiple screen sessions detached and you are connecting to a specific one.

You should find your still running for loop, chugging away.

  • linux_wiki/screen.1416793908.txt.gz
  • Last modified: 2019/05/25 23:50
  • (external edit)