How to Keep a Linux Terminal Session Alive Using the screen Command

When managing a remote Linux server via SSH, network stability is a constant concern. If you are compiling a massive software package, migrating a massive database, or running a backup script that takes four hours to complete, a split-second drop in your Wi-Fi connection or your laptop accidentally going to sleep will sever the SSH connection. When the connection drops, the server immediately kills all processes tied to that session, completely ruining your four hours of work.

To prevent this catastrophe, system administrators use a terminal multiplexer called screen. This utility creates an indestructible, virtual terminal window that continues running in the background on the server, even if you disconnect, close your laptop, or lose internet access entirely.

What is the screen Command?

Think of screen as a virtual monitor running inside the server. When you run a command inside a screen session, the command is tied to the virtual monitor, not your SSH connection. If your SSH connection dies, the virtual monitor remains perfectly safe in the server’s memory, quietly continuing the work until you log back in to check on it.

How to Install and Start a Screen Session

Most modern Linux distributions include screen by default. If yours does not, you can install it via your package manager (e.g., sudo apt install screen or sudo dnf install screen).

To start a new virtual session, simply type the command:

screen

Your terminal will clear, and you will see a fresh command prompt. It looks exactly like a normal terminal, but you are now safely inside the virtual environment. Any command you run here is protected.

Detaching: Leaving the Session Running

Let’s assume you start a massive database backup script. You do not want to stare at the screen for three hours. You want to safely “detach” from the screen, leaving the script running in the background, so you can log off.

To detach, you must use a specific keyboard shortcut sequence:

  1. Press and hold the Ctrl key.
  2. Tap the A key (This tells screen you are about to give it a command).
  3. Release the A key, and tap the D key (for Detach).

Your terminal will drop you back to your original SSH prompt with a message stating [detached]. Your backup script is now running safely in the background. You can safely close your terminal or turn off your computer.

Reattaching: Returning to the Session

Three hours later, you log back into the server via SSH. You want to see if the backup finished.

To reattach to the running screen session, use the “resume” flag:

screen -r

You will instantly be teleported back into the virtual terminal, exactly as you left it. You can see the output of the backup script and continue working.

Managing Multiple Screens

If you are a power user, you might have five different screen sessions running different tasks simultaneously. If you type screen -r, the server will not know which one you want to join, and will instead print a list of active sessions with unique ID numbers (e.g., 14592.pts-0.server).

To list all active sessions at any time, type:

screen -ls

To reattach to a specific session, type screen -r followed by the ID number:

screen -r 14592

Closing a Session Permanently

Once your massive task is completely finished and you no longer need the virtual terminal, you should close it to free up server memory. Reattach to the screen session, ensure your scripts are finished, and simply type the standard exit command:

exit

The screen will terminate, and you will be dropped back to your normal SSH session with a message stating [screen is terminating].

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.

Receive our best articles and tips delivered straight to your inbox.