The command line lets you run commands and programs. Some commands, like
ls, are fast. Others, like
sudo apt install,
may take a while to complete. You often want to monitor the progress of such
It’s usually not very hard to keep an eye on what’s happening: just wait until the command finishes. But sometimes this isn’t possible, for instance when you’re about to turn off your computer or have a really bad internet connection. Once you disconnect from the server the command either stops executing or keeps running invisibly in the background, with no easy way to “go back”.
In such situations you may want to use GNU Screen (or simply
utility comes pre-installed on most servers and can be used to run multiple
at the same time. It also enables you to run programs after you disconnect from
Follow the three steps below to use
Let’s say you want to run a command that will take a long time to complete. You
want to keep an eye on it, but you don’t have time to wait for it right now.
If you want to run this command using
screen, you first need to start a new
Technically this can be done by simply issuing the
screen command, but don’t
do this! By default
screen generates session names that may be hard to
Instead, give your session a nice name, so that you (or others who have access to the same machine) can easily recognise it:
$NAME with whatever you want. Ideally, pick a simple name that
describes why you started the session or that mentions your name. Try to avoid
spaces and special characters (other than
.), as these can make it
harder to return to the session later.
For example, to start a session named
upgrade-database, execute the following
If done correctly, you should now see an empty screen. This means that you have started a new session. , like that command that takes ages to run.
At some point (usually directly after executing a long-running command), you’ll want to leave your session. There are two ways to do this.
It may help to think of sessions as application windows. As long as you keep a window open, everything in that window continues to work as normal.
When you’re temporarily done with a window, you can minimise it. In
terminology this is called detaching. To detach from a session, press
A, followed by
If you’re using a terminal emulator like Windows Terminal or iTerm2, you can
also simply close the tab to detach from the session.
Alternatively, once the command has finished and you no longer need the session,
you can completely end it by pressing
D. This is more
akin to closing a window: if you do this, the session is gone and you can’t
return to it.
When you detach from a session it’s still active in the background. Use
screen -r to reattach to an existing session:
$SESSION_NAME is the name that you previously entered when you started the
screen -S. For example:
If you forgot the name of your session, you can use
to see a list of sessions that are still active:
Note that each session name has . This number can be used instead of the name when reattaching to a session. In fact, when two or more sessions share the same name, this is the only way to reattach to the right session:
In practice you’ll rarely encounter situations where you need something other
than the three steps I showed above. But you want to learn more about
you can always use
screen --help to display all the available options: