How to Use the ‘screen’ Command to Keep Terminal Sessions Alive on Remote Servers

The Danger of Disconnected SSH Sessions

If you connect to a remote Linux server via SSH and initiate a long-running task-such as compiling a large software package, running a database backup, or transferring massive files-you must keep your terminal window open and your computer connected to the internet until the task finishes. If your Wi-Fi drops for even a second, or if your laptop goes to sleep, the SSH connection breaks. When the SSH connection breaks, the Linux server immediately terminates any processes attached to that session, instantly ruining your hours-long backup or compilation.

To solve this, Linux administrators use terminal multiplexers. The oldest and most widely available multiplexer is the screen command. screen creates virtual terminal sessions that run completely independent of your physical SSH connection. If you disconnect, the virtual screen keeps running in the background, allowing you to reconnect later and pick up exactly where you left off.

Basic Usage: Creating and Detaching Screens

Step 1: Start a New Screen Session

Most Linux distributions come with screen pre-installed. To start a new virtual session, simply type:

screen

You will see a brief welcome message. Press Space or Enter to clear it. You are now inside the virtual session. It looks exactly like a normal terminal, but you are protected.

Pro Tip: It is highly recommended to name your sessions so you can identify them later. Use the -S flag:

screen -S database_backup

Step 2: Start Your Task

Inside this new screen, start whatever long-running command you need to execute (e.g., mysqldump or tar).

Step 3: Detach from the Screen

This is the most important step. To leave the screen running in the background and return to your primary SSH session, you must input a specific keyboard shortcut. Do not type the word “exit” or close the window.

Press and hold Ctrl, then press A, then press D (for Detach).

You will be dropped back to your original terminal, and you will see a message like [detached from 12345.database_backup]. Your long-running task is now safely insulated. You can close your laptop, turn off your Wi-Fi, or close the terminal window entirely.

Resuming (Reattaching) to a Screen

When you want to check on the progress of your task, reconnect to your server via SSH.

Step 1: List Available Screens

To see all the screens currently running in the background, type:

screen -ls

You will see output detailing the Process ID (PID) and the name of the session, e.g., 12345.database_backup (Detached).

Step 2: Reattach

To reconnect to that specific session, use the -r flag followed by the name (or just the PID):

screen -r database_backup

Your terminal will instantly snap back to exactly how you left it, showing the live progress of your task.

Killing a Screen Session

Once your task is finished, you do not want to leave the screen running forever, as it consumes a tiny amount of system memory.

  1. Reattach to the screen (screen -r session_name).
  2. Ensure your task is finished.
  3. Simply type exit and press Enter.

You will see the message [screen is terminating], and the virtual session will be destroyed.

Conclusion

Never run a critical, long-duration command directly in a bare SSH session. By wrapping your tasks in the screen utility, you decouple your workflow from the physical stability of your network connection, ensuring that network blips never destroy your server administration tasks.

Get the best tech tips delivered straight to your inbox.

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