GNU screen is a useful tool, it has been mentioned in multiple earlier articles as a good way to keep a program running after you log out of a server.
Screen runs commands in sessions. Each session can have multiple screens in which commands are run, which can be a more useful interface to multiprocessing than running jobs in the background.
A screen session can be connected to by multiple terminals, and can be detached from entirely, so there's no user reading the command's output.
Creating a session
The simplest use of screen is to simply enter screen
at which point
it creates a new session, running your shell.
Commands other than the default of your shell can be executed too. You
can run screen tail -f /var/log/foo.log
to watch the logged output of
a command.
You can specify the name of the session with the -S
argument, which
can be used later to re-join or kill a session by name.
Entering commands
Screen has many default key-bindings for running commands. These are
mostly prefixed by the escape key. This defaults to ^A
(read as holding
Ctrl an pressing the a key).
^A
is commonly used by emacs users to move the cursor to the
beginning of the line. It is possible to re-bind to a different key by
putting escape $KEY
in your ~/.screenrc
.
escape ^Bb
will make screen use ^B
for its escape character, rather
than ^A
.
Commands that had not been bound can be entered by entering the ^A:
command (hold Ctrl, press a, then press :). This will open a command
line interpreter, where you can enter other commands.
Opening a new screen in your session
The screen
command will open a new screen in the same session. The
keyboard shortcut for this is to enter ^Ac
, which opens a new screen
and runs a shell in it.
To run a specific command in your new screen, just like how we create the initial screen session, just in the screen command line, rather than your shell's command line.
For example, entering screen tail -f /var/log/foo.log
in the command
line opened by pressing ^A:
, will open a new screen and read the output
of foo.log
into it, which can be used to monitor the logs of a command
being run in another screen.
Switching screens
^Aa
will switch between your current screen and your previous screen.
Your open screens are listed in the caption at the bottom of the
screen. Pressing ^A$NUMBER
will switch to that screen, so ^A1
will
switch to the first screen.
^A"
will open a menu of screens to switch to, including what command
is running in that screen, which is useful if the information in the
caption bar isn't sufficient.
The command to switch screens is called select
, so running select 1
in screen's command line will switch to screen 1.
Splitting screens
Using screen to manage multiple commands is already an improvement over bash's job control.
- The caption bar shows you which commands are running
- You don't need to background a command to have multiple running
- The output from multiple commands doesn't get interleaved into an incomprehensible mess, since the output is separated by screen.
However, we currently can only see the output of one command at a time, so we can't use our earlier example of looking at the logged output of the foo command, while running other commands.
To handle this, screen allows you to split your view into multiple
windows.. ^As
produces a horizontal split, so you can have one window
at the top, and a different below it.
^A|
creates a vertical split, so you can have a different window on
the left and the right.
Various windows can be cycled between with the ^A<TAB>
key.
^AX
will remove the current split, and ^AQ
will remove all windows
except the one which currently has focus.
Closing screens in your session
When the primary process of a screen terminates, the associated screen
will close, so if you enter exit
on your shell, the screen will close.
If a screen is stubbornly refusing to exit, you can run ^Ak
to kill
the current screen.
Closing a session
When all commands run in a screen session exit, the session is closed.
If a command is not easily closed or you do not care for cleanly closing
it, then you can press the ^A\
shortcut to kill everything, or entering
the quit
on the screen command line.
Detaching from a session
A screen session can be left running without any user being connected
to it. This can be accomplished by creating one as before, and pressing
^AD
to detach yourself from it.
A screen can be started in detached mode by starting it as screen -d -m
.
Joining an existing session
screen -x
will join an existing session, even if there is currently
a user already attached. This allow multiple users on the same screen
session, but can behave strangely if each user has a different terminal
size.
The latest session with no users can be re-attached to with screen
-R
. This can fail if the latest screen session has a user, so there's
the -d
option too, which will detach the session first if necessary.
The -D
command is useful, since you can also provide the command
you want to run as the first argument, so screen -D -R tail -f
/var/log/foo.log
will detach and re-attach to an existing session if
it exists, otherwise it will create a new one reading log output.