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:
[root@home ~]# 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) [root@home ~]# yum install screen ... complete! #deb-based (ubuntu/debian) [root@home ~]# apt-get install screen ... complete!
screen is started from the command line just like any executable:
[root@home ~]# screen #or you can start it with a custom name: [root@home ~]# 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”
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.
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.
if your connection drops or you have detached from a screen, you can re-attach by just running:
[travis@home ~]$ screen -r
this will re-attach to your screen.
however, if you have multiple screens you may get this:
[travis@home ~]$ 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.
[travis@home ~]$ screen -r 31844.pts-0.home
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” “_”.
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.
[travis@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
travis@home:/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:
travis@home:/# screen -s 8222 -x quit travis@home:/# screen -list no sockets found in /var/run/screen/s-travis.
when you're a little too careless about virtualizing your domain controllers, cloning, migrating, backing up and restoring, returning from vacation… Read More
systemd is new service manager for linux. it's a replacement for all previous init systems (sysv/sysvinit & ubuntu's upstart) and… Read More