Linux Screen allows you to:
- Use multiple shell windows from a single SSH session.
- Keep a shell active even through network disruptions.
- Disconnect and re-connect to shell sessions from multiple locations.
- Run a long running process without maintaining an active shell session.
Here is a quick rundown from the man page:
screen – screen manager with VT100/ANSI terminal emulation
screen [ -options ] [ cmd [ args ] ]
screen -r [[pid.]tty[.host]]
screen -r sessionowner/[[pid.]tty[.host]]
Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells). Each virtual terminal provides the functions of a DECVT100 terminal and, in addition, several control functions from the ISO 6429 (ECMA 48, ANSI X3.64) and ISO 2022 standards (e.g. insert/delete line and support for multiple character sets). There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows moving text regions between windows.
When screen is called, it creates a single window with a shell in it (or the specified command) and then gets out of your way so that you can use the program as you normally would. Then, at any time, you can create new (full-screen) windows with other programs in them (including more shells), kill existing windows, view a list of windows, turn output logging on and off, copy-and-paste text between windows, view the scroll‐back history, switch between windows in whatever manner you wish, etc. All windows run their programs completely independent of each other. Programs continue to run when their window is currently not visible and even when the whole screen session is detached from the user’s terminal. When a program terminates, screen (per default) kills the window that contained it. If this window was in the foreground, the display switches to the previous window; if none are left, screen exits. Shells usually distinguish between running as login-shell or sub-shell. Screen runs them as sub-shells, unless told otherwise (See
"shell" .screenrc command).
Everything you type is sent to the program running in the current window. The only exception to this is the one keystroke that is used to ini‐tiate a command to the window manager. By default, each command begins with a control-a (abbreviated C-a from now on), and is followed by one other keystroke. The command character and all the key bindings can be fully customized to be anything you like, though they are always two characters in length.
Chances are that you already have screen on your system. On most RPM based distributions you can find Screen in
/usr/bin/screen. To see if screen is in your path, you can use the which command:
[[email protected] ~]# which screen /usr/bin/screen
If you do not have Screen, then you can most likely install it from your distribution repository. If it’s not there you can build from source from the official GNU site https://www.gnu.org/software/screen/.
#rpm-based (RHEL/CentOS) [[email protected] ~]# yum install screen ... Complete! #deb-based (Ubuntu/Debian) [[email protected] ~]# apt-get install screen ... Complete!
Starting Linux Screen
Screen is started from the command line just like any executable:
[[email protected] ~]# screen #Or you can start it with a custom name: [[email protected] ~]# screen -S screenname
You are now inside of a screen session. This functions just like a normal session except you can now use special key sequences to perform certain actions, such as detaching from the screen session while leaving the session running which is explained below.
Screen uses the command “Ctrl-a”that’s the control key and a lowercase “a” as a signal to send commands to screen instead of the shell. The most common is “Ctrl-a-d” which detaches the running screen session and leaves you free to exit the shell and walk away, or go home and resume the session.
Command: “Ctrl-a” “c”.
To create a new window, you just use “Ctrl-a” “c”.
This will create a new window for you with your default prompt. Your old window is still active.
For example, I can be running top and then open a new window to do other things. Top stays running! It is still there. To try this for yourself, start up screen and then run top. (Note: I have truncated some screens to save space.)
top - 09:10:33 up 35 days, 17:26, 1 user, load averag Tasks: 131 total, 1 running, 130 sleeping, 0 stoppe Cpu(s): 0.4%us, 0.2%sy, 0.0%ni, 99.4%id, 0.0%wa, 0 Mem: 12302040k total, 6363652k used, 5938388k free, Swap: 1052248k total, 12k used, 1052236k free
Now open a new window with: “Ctrl-a” “c”
Switching Between Windows
Command: “Ctrl-a” “n”
Screen allows you to move forward and back. In the example above, you could use “Ctrl-a “n” to get back to
top. This command switches you to the next window.
The windows work like a carousel and will loop back around to your first window.
You can create several windows and toggle through them with “Ctrl-a” “n” for the next window or “Ctrl-a” “p” for the previous window.
Detaching From Screen
Command: “Ctrl-a” “d”
This is biggest reason to use Screen. This will detach your current session which leaves everything running. You can then go home, watch Netflix all weekend, come back on Monday and reattach to your existing session as long as the server has not restarted.
If your network connection fails, screen will automatically detach your session!
You can detach from the window using “Ctrl-a” “d”.
This will drop you into your shell, from which you should type exit (maybe twice if you
sudo su‘d) again to logout from the system.
All screen windows are still there and you can re-attach to them later.
This is great when you are using
rsync for server migration.
Reattach to Screen
If your connection drops or you have detached from a screen, you can re-attach by just running:
[[email protected] ~]$ screen -r
This will re-attach to your screen.
However, if you have multiple screens you may get this:
[[email protected] ~]$ screen -r There are several suitable screens on: 12893.pts-1.home (Detached) 38225.pts-0.home (Detached) Type "screen [-d] -r [pid.]tty.host" to resume one of them.
If you get this, just specify the screen you want.
[[email protected] ~]$ screen -r 31844.pts-0.home
Logging Your Screen Output
As a consultant, I find it important to keep track of what I do to someone’s server. Fortunately, screen makes this easy.
Using “Ctrl-a” “H”, creates a running log of the session.
Screen will keep appending data to the file through multiple sessions. Using the log function is very useful for capturing what you have done, especially if you are making a lot of changes. If something goes awry, you can look back through your logs.
Screen can monitor a window for activity or inactivity. This is great if you are downloading large files, compiling, or waiting for output.
If you are waiting for output from a long running program, you can use “Ctrl-a” “M” to look for activity. Screen will then flash an alert at the bottom of the page when output is registered on that screen.
I use this when running a command that takes a long time to return data. I can just fire up the command, switch to another window and not have to keep switching back to check the status.
You can also monitor for inactivity. Why use this?
If you are downloading a large file or compiling a program, you can be notified when there is no more output. This is a great signal to when that job is done. To monitor for silence or no output use “Ctrl-A” “_”.
Locking Your Screen Session
If you need to step away from your computer for a minute, you can lock your screen session using “Ctrl-a” “x”. This will require a password to access the session again.
[[email protected]home ~]$ screen -r 12362.pts-0.home
When you are done with your work, I recommend you stop the session instead of saving it for later. To stop screen you can usually just type
exit from your shell. This will close that screen window. You have to close all screen windows to terminate the session.
You should get a message about screen being terminated once you close all windows.
[screen is terminating]
Alternatively, you can use “Ctrl-a” “k”. You should get a message if you want to kill the screen.
To force kill a screen you should make a note of the session ID/name with
[email protected]:/home/ubuntu# screen -list There is a screen on: 8222.htop (10/31/2016 02:14:55 AM) (Detached) 1 Socket in /var/run/screen/S-travis.
Now you can kill the session the proper way using the quit command instead of kill like this:
[email protected]:/# screen -S 8222 -X quit [email protected]:/# screen -list No Sockets found in /var/run/screen/S-travis.