Hostxpeed
Login Get Started →
Getting Started

How to Use Screen for Long Tasks

6 min read
22 views
Jun 10, 2026

Prerequisites

Before using screen, make sure you have:

  • SSH access to your VPS
  • Screen installed on your server

💡 Screen allows you to detach a session and reattach later, keeping your processes running even after SSH disconnect.

Step 1: Install Screen

Ubuntu/Debian:

apt update
apt install screen -y

CentOS/Rocky:

yum install screen -y

Step 2: Start a New Screen Session

screen -S taskname

Replace taskname with a descriptive name (e.g., backup, upgrade, download).

You will now be in a new screen session (looks like a normal terminal).

Step 3: Run Your Long Task

apt upgrade -y

Or:

wget https://largefile.com/backup.tar.gz

Or any long-running command.

Step 4: Detach from Screen (Ctrl+A, then D)

Press:

Ctrl + A, then D

You will see:

[detached from 12345.taskname]

Your task continues running in the background.

Step 5: Exit SSH Safely

exit

Your screen session keeps running on the server.

Step 6: Reattach to Your Screen SessionLater, reconnect via SSH:

ssh hxroot@YOUR_SERVER_IP -p 22

List available screen sessions:

screen -ls

Output example:

There is a screen on:
    12345.taskname   (04/28/2026 10:30:15 AM)   (Detached)
1 Socket in /run/screen/S-root.

Reattach:

screen -r 12345

Or by name:

screen -r taskname

Common Screen Commands

Within a screen session (Ctrl+A prefix):

  • Ctrl+A, D - Detach session
  • Ctrl+A, C - Create new window
  • Ctrl+A, N - Next window
  • Ctrl+A, P - Previous window
  • Ctrl+A, " - List windows
  • Ctrl+A, K - Kill current window
  • Ctrl+A, ? - Help

Multiple Windows in One Screen Session

Create a new window:

Ctrl+A, C

Switch between windows:

Ctrl+A, N    (next)
Ctrl+A, P    (previous)
Ctrl+A, 0-9  (window number)

Name your windows:

Ctrl+A, A

Kill a Screen Session

From inside screen:

exit

Or from outside:

screen -X -S taskname quit

Force Reattach (If Session is Attached Elsewhere)

screen -d -r taskname

-d detaches the other session, -r attaches this one.

Log Screen Output to File

Start screen with logging:

screen -L -S taskname

Log file: screenlog.0 in your current directory.

Enable logging for all sessions:

echo "logfile /root/screenlogs/screen-%S.log" >> ~/.screenrc
echo "deflog on" >> ~/.screenrc

Real-World Use Cases

  • Running long database migrations
  • Downloading large files
  • Compiling software/kernels
  • Running backups (mysqldump, rsync)
  • System upgrades that take hours
  • Running scripts overnight

Script Example: Auto-start Screen Session

#!/bin/bash
screen -dmS backup_session bash -c 'rsync -av /var/www /backup/; exec bash'

Creates a detached screen session running rsync backup.

Common Issues

Screen not installed: Install with apt or yum.

Cannot reattach: Try screen -d -r taskname

Screen session frozen: Kill with screen -X -S taskname quit

✅ You can now run long tasks that survive SSH disconnections using GNU Screen.

Was this article helpful?