Chuniversiteit logomarkChuniversiteit.nl
Living the Pipe Dream

Execute commands and long-running scripts in the background using screen

There’s an easy way to keep scripts and commands “alive” when you disconnect from a server.

A man grabs a desktop PC monitor from a server rack
screen lets you pick up things right where you left them

The command line lets you run commands and programs. Some commands, like cd or ls, are fast. Others, like mysqldump, scp, or sudo apt install, may take a while to complete. You often want to monitor the progress of such long-running commands.

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 screen). This utility comes pre-installed on most servers and can be used to run multiple programs at the same time. It also enables you to run programs after you disconnect from the server.

Follow the three steps below to use screen:

  1. Begin a new session
  2. Temporarily detach from the session
  3. Resume your session later

Start

Link

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 session.

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 remember, like 2041652.pts-3.vs-cza-d41d48.

Instead, give your session a nice name, so that you (or others who have access to the same machine) can easily recognise it:

Replace $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 - and .), as these can make it harder to return to the session later.

For example, to start a session named upgrade-database, execute the following command:

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.

Stop

Link

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 screen terminology this is called detaching. To detach from a session, press Ctrl + A, followed by Ctrl + D. 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 Ctrl + D. This is more akin to closing a window: if you do this, the session is gone and you can’t return to it.

Continue

Link

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 session via screen -S. For example:

If you forgot the name of your session, you can use screen -ls 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:

But wait, there’s more

Link

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 screen, you can always use screen --help to display all the available options: