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
screen tail -f /var/log/foo.log to watch the logged output of
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.
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
escape $KEY in your
escape ^Bb will make screen use
^B for its escape character, rather
Commands that had not been bound can be entered by entering the
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
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
foo.log into it, which can be used to monitor the logs of a command
being run in another screen.
^Aa will switch between your current screen and your previous screen.
Your open screens are listed in the caption at the bottom of the
^A$NUMBER will switch to that screen, so
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
in screen's command line will switch to screen 1.
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
^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
^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
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
The latest session with no users can be re-attached to with
-R. This can fail if the latest screen session has a user, so there's
-d option too, which will detach the session first if necessary.
-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.