You run long processes over SSH, perhaps over the WAN, and when you get disconnected you lose a lot of work. Or perhaps you started a long job from work, but need to go home and be able to check on the job later; you could run your process using nohup, but then you won’t be able to reattach to it when your connection comes back or you get home.
Using screen is very simple. Type screen
or screen
-a
. The -a
option includes
all of screen’s capabilities even at the expense of
some redraw (thus bandwidth) efficiency. Honestly, we use -a
but have never noticed a difference.
When you do this, it will look like nothing happened, but you are
now running inside a screen. echo
$SHLVL
should return a number greater than one if this worked
(see also :L$SHLVL
in Customizing Your Prompt). To test it, do an ls -la
, then kill
your terminal (do not exit cleanly, as
you will exit screen as well). Log back into the
machine and type screen -r
to
reconnect to screen. If that doesn’t put you back
where you left off, try screen -d -r
.
If that doesn’t work, try ps auwx | grep
[s]creen
to see if screen is still
running, and then try man screen
for
troubleshooting information—but it should just work. If you run into
problems with that ps command on a system other
than Linux, see Finding Out Whether a Process Is Running.
Starting screen with something like the
following will make it easier to figure out what session to reattach to
later if necessary: screen -aS
"$(whoami).$(date' +%Y-%m-%d_%H:%M:%S%z')
“. See the
run_screen script in Getting Started with a Custom Configuration.
To exit out of screen and your session, keep
typing exit
until all the sessions
are gone. You can also type Ctrl-A Ctrl- or Ctrl-A :quit
to exit screen
itself (assuming you haven’t changed the default meta-key of Ctrl-A
yet).
According to the screen web site:
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 DEC VT100 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.
What that means is you can have more than one session in a single SSH terminal (think DeskView on i286/386). But it also allows you to SSH into a machine, start a process, disconnect your terminal and go home, then reconnect and pick up—not where you left off, but where the process has continued to. And it allows multiple people to share a single session for training, troubleshooting, or collaboration (see Sharing a Single bash Session).
screen is often installed by default on Linux but rarely on other
systems. The screen binary must run SUID root so
it can write to the appropriate usr/dev
pseudoterminals (tty). If screen doesn’t work,
this is a likely reason why (to fix it, enter chmod u+s /usr/bin/screen
as
root).
screen interferes with in-line transfer protocols like zmodem. Newer versions of screen have configuration settings that deal with this; see the manpages.
The default Emacs mode of bash command-line editing uses Ctrl-A to go to the start of the line. That’s also the screen command mode, or metakey, so if you use Ctrl-A a lot like we do, you may want to add the following to your ~/.screenrc file:
# Sample settings for ~/.screenrc # Change the C-a default to C-n (use C-n n to send literal ^N) escape ^Nn # Yes annoying audible bell, please vbell off # detach on hangup autodetach on # make the shell in every window a login shell shell -$SHELL
18.219.4.174