Screen is billed as a “full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells)” (from the man page). Linux admins use screen for different purposes, but the most common is the ability to run long processes on remote servers – with the ability to disconnect, then reconnect later to see how things are going.
Other uses are to start a program/process then share it out, or allow another tech to sign in and take over. Whatever the reason to use screen, this little ‘howto’ will hopefully show you some of the basics and get you started.
Before we begin, there are other similar tools out there that provide the same (and in some cases, more) features. We’ll go into these in another article, but you could also check out tmux and byobu.
Screen Basics:
First, get into a shell on a Linux machine somewhere. At the prompt type screen:
|
1 |
rob@rob-X551M:~$ screen |
Now, you’re in a screen! You can tell you’re in a screen if you echo your $STY:
|
1 2 |
rob@rob-X551M:~$ echo $STY 5993.pts-1.rob-X551M |
Start up a program that will keep running until you kill it.. like top:
|
1 |
rob@rob-X551M:~$ top |
Ok, now let’s leave that running and detach the ‘screen’ so we can come back to it later on…
Use the keystroke: ctrl-a d
You should be back at your prompt again and top would be gone.. You may see something like:
|
1 2 |
[detached from 5993.pts-1.rob-X551M] rob@rob-X551M:~$ |
Hey, the server seems slow.. let’s check out top and see what’s going on.. to re-connect to your screen session, type: screen -r
|
1 |
rob@rob-X551M:~$ screen -r |
Now, you should be back in your screen again.
Ok, now quit out of top (ctrl-c) and let’s kill this screen with ctrl-a k. You’ll see something like:
|
1 2 |
[screen is terminating] rob@rob-X551M:~$ |
Getting fancy:
Let’s get a little fancy.. you can think of a couple of things you’re going to use screen for.. maybe one is irc.. maybe another is a software build on a remote server.. so let’s open a couple screens and name them as we go.
Start a screen that is named ‘irc’:
|
1 |
screen -S irc |
Now, let’s detach it.. ctrl-a d
Start another screen called ‘build’
|
1 |
screen -S build |
Let’s detach that one.. ctrl-a d.
We have two screens out there now. You can always see if there are any screens hanging out by listing them:
|
1 |
screen -ls |
You should see something similar to:
|
1 2 3 4 5 6 |
There are screens on: 6222.build (06/25/2014 10:44:29 AM) (Detached) 6172.irc (06/25/2014 10:44:21 AM) (Detached) 2 Sockets in /var/run/screen/S-rob. rob@rob-X551M:~$ |
Let’s attach to the irc one:
|
1 |
screen -r irc |
BOOM – you’re in your IRC screen again!
You could also create a new ‘window’ once in screen then cycle through them if you like.
Exit out of that screen (ctrl-a d to detach or ctrl-a k to kill it) and let’s start a new screen called ‘test’
|
1 |
screen -S test |
Same as before.. you’re in a new screen called test. Let’s add a window to this screen session:
|
1 |
ctrl-a c |
Now you’re running two windows in your screen session! Type something to get some output here.. maybe start top again.. once it’s going, switch to your other window:
|
1 |
ctrl-a n |
Hit enter a couple times to see some output.. then switch back to your window running ‘top’ with ctrl-a n again..
You can also switch to a specific shell number.. for instance: ctrl-a 2, ctrl-a 1
Ok, that’s probably enough to get you started / in trouble. For more information on screen, you can visit the official GNU screen home: http://www.gnu.org/software/screen/.







