How to Use the Linux screen Command for Persistent Detached Terminal Sessions

The Broken Pipe Disaster

One of the most frustrating experiences in Linux administration occurs during long-running tasks. Suppose you SSH into a remote production server to execute a massive database backup script that takes four hours to complete. Two hours into the process, your local laptop connects to a different Wi-Fi network, or your VPN drops for a single second.

The SSH connection terminates with a “Broken Pipe” error. Crucially, when an SSH session disconnects, the Linux kernel sends a SIGHUP (Hangup) signal to every child process attached to that session. Your four-hour database backup script is instantly and violently killed, potentially leaving the database in a corrupted state, forcing you to start entirely over.

To completely immunize long-running commands from network instability, UNIX engineers use the screen command. screen acts as a virtual terminal multiplexer. It launches a persistent, invisible shell session that lives entirely on the remote server, completely decoupled from your physical SSH connection. If your laptop loses power, the screen session continues executing the backup script in the background. When your laptop reboots, you simply reconnect to the remote server and “re-attach” to the screen, picking up exactly where you left off.

Step 1: Installing and Launching Screen

The screen utility is often installed by default, but if it is missing, it is available in the standard repositories:

sudo apt update
sudo apt install screen -y

To start a new virtual session, simply type:

screen

The terminal will briefly flash a copyright message, and you will be presented with a standard bash prompt. It looks identical to your normal terminal, but you are now operating entirely inside the protected virtual session.

Step 2: Executing Tasks and Detaching

Inside the screen session, you can execute your long-running task:

mysqldump -u root -p massive_database > /backup/massive_database.sql

Now, to prove the persistence of the session, you want to safely disconnect (detach) from the screen without killing the script.

You must use the keyboard shortcut: Ctrl+A, followed by d.

(Press Control and ‘A’ simultaneously, release both, then press the ‘d’ key).

The terminal will instantly drop you back to your primary SSH session, displaying a message like [detached from 12345.pts-0.server].

Your database backup is now running safely in the background. You can type exit to completely sever your SSH connection to the server and turn off your laptop.

Step 3: Re-attaching to a Session

Hours later, you SSH back into the server. To check on the status of your backup script, you must re-attach to the virtual session.

First, list all currently running screen sessions:

screen -ls

The output will show the Process ID (PID) of your detached session:

There is a screen on:
        12345.pts-0.server  (Detached)
1 Socket in /run/screen/S-admin.

To re-attach to this specific session, use the -r (resume) flag followed by the PID:

screen -r 12345

The terminal will instantly snap back to the virtual session, displaying the exact output of the backup script, as if you had never left.

Step 4: Naming Sessions for Sanity

If you are managing a complex server, you might have five different screen sessions running simultaneously (one for a backup, one tailing logs, one running a Python script). Navigating them by random numerical PIDs (like 12345) is confusing.

When launching a new screen, always use the -S (Session Name) flag to assign a human-readable identifier:

screen -S database_backup

When you detach (Ctrl+A, d) and run screen -ls, the name will be clearly displayed. You can then re-attach using the human-readable name instead of the PID:

screen -r database_backup

Step 5: Sharing Sessions (Multi-user Mode)

screen is not just for persistence; it is an incredible collaboration tool. Two different administrators can attach to the exact same screen session simultaneously.

If you are training a junior administrator, you can start a screen session. The junior administrator SSHes into the server (using the same user account) and runs the attach command using the -x flag (which allows attaching to a session that is already attached elsewhere):

screen -x database_backup

Instantly, their terminal mirrors yours. If you type a command, they see the keystrokes appear on their screen in real-time. If they type a command, you see it. It is the ultimate command-line pair-programming tool.

Step 6: Terminating a Session

Once your massive backup script finishes, you no longer need the virtual session holding up system resources.

To permanently destroy a screen session, simply type the standard exit command while attached to the screen:

exit

The screen will close, returning you to your primary SSH shell, and displaying [screen is terminating].

Conclusion

Executing critical, long-running administrative tasks over a fragile SSH connection is a massive operational risk. By wrapping commands in the persistent, detached architecture of the screen utility, Linux engineers can ensure that dropped VPNs and sleeping laptops never cause catastrophic interruptions to server maintenance workflows.

RELATED POSTS

  • How to Use the diff Command to Compare Two Text Files Line by Line in Linux
  • How to Prevent a Linux System from Sleeping via Terminal
  • How to Sort Data in Linux Using the sort Command
  • How to Use the Linux ulimit Command to Restrict Process Resource Usage
  • How to Use the Linux lsattr Command to View File Attributes
  • Get the best tech tips delivered straight to your inbox.

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