How to Use screen to Keep an Application Running After Closing SSH

Learn to use screen on Linux to keep scripts and applications running after disconnecting from SSH, with persistent and reattachable sessions.

Every time you open an SSH connection and run a long script — a build, a model training run, a deploy, anything that takes more than a few minutes — you become hostage to the connection. Network drops, closing the laptop lid, router timeouts: any of these events kills the SSH, and when that happens, the process running in the foreground receives a SIGHUP and dies along with it.

screen solves this problem by decoupling the process from the interactive terminal. You create a session, run what you need inside it, detach, close the SSH, and walk away. The process keeps running indefinitely on the server. When you come back — minutes or days later — you reattach the session and you are exactly where you left off, with the full output preserved.

This tutorial covers installation, the essential commands, and the shortcuts you’ll use 90% of the time. Estimated execution time: 10 to 15 minutes, including practice.

Prerequisites

Prerequisites

You need a Linux machine with working SSH access (any modern distribution will do: Ubuntu 22.04+, Debian 12+, Rocky 9, Alma 9). sudo access is required only to install the package — using screen itself runs as a regular user.

Package screen
Size ~600 KB
Dependencies libc, libutempter
Target version 4.x or higher

Installing screen

On most production servers, screen comes installed by default. Check first before installing.

01

Verify whether screen is already available:

which screen && screen --version

If the command returns a path (/usr/bin/screen) and the version, skip to the next section. If it returns empty or “command not found”, proceed with the installation.

02

Install on Debian/Ubuntu-based systems:

sudo apt update
sudo apt install -y screen

On Red Hat-family distributions (Rocky Linux, AlmaLinux, CentOS Stream):

sudo dnf install -y screen
03

Confirm the installation:

screen --version

The expected output is something like Screen version 4.09.00 (GNU) — any 4.x version will do. 3.x versions still work but are rare these days.

Creating your first session

Creating named sessions is the recommended pattern — it makes them easier to identify and reattach later.

01

Create a named session. Use a descriptive name for what will run inside:

screen -S deploy

You’ll notice a “clean” screen — it looks like a normal terminal, but you are now inside a screen session. Run any long-running command here. As an example:

ping -c 1000 8.8.8.8
02

Detach the session without killing the process. This is the most important operation in screen — the shortcut Ctrl+a followed by d:

Press Ctrl+a (hold Ctrl, tap a, release both), then press d alone.

You return to the original shell. The message [detached from 12345.deploy] confirms the session keeps running in the background. You can even close the SSH connection now — the ping keeps counting inside.

The shortcut that defines screen

Almost every screen command starts with the Ctrl+a prefix. It’s called the “command key” and works like a mode: you press Ctrl+a to enter command mode, and the next key is the action. Ctrl+a d = detach. Ctrl+a c = create new window. Ctrl+a ? = full help.

Listing and reattaching sessions

After detaching, you need to know which sessions exist before reattaching to any of them.

01

List all your active sessions:

screen -ls

The output shows each session with PID, name, and state:

There are screens on:
        12345.deploy    (Detached)
        12890.build     (Detached)
2 Sockets in /run/screen/S-root.

The number before the dot is the daemon PID, and what comes after is the name you gave it. Common states: (Detached) = idle, waiting to be reattached, (Attached) = someone is connected right now, (Dead) = dead but with an orphaned socket (clean it up with screen -wipe).

02

Reattach the session. Use the name or the PID:

screen -r deploy

You’ll land exactly where you left off — all process output, cursor position, command history. Everything preserved. If there is only one detached session, screen -r with no arguments handles it.

03

Force reattach. If you forget to detach and open a new SSH connection trying to reattach, you’ll get the error There is no screen to be resumed matching .... Solve it by forcing:

screen -dr deploy

The -d flag forces a detach of the current session (even if another terminal is attached) before reattaching to yours. Use -D -RR for “force detach, attach, create if not exists” — useful in scripts.

Essential commands during a session

Once inside the session, a few extra shortcuts will make daily use easier.

ShortcutAction
Ctrl+a dDetach the session (leaves it running in background)
Ctrl+a cCreate a new “window” inside the session
Ctrl+a nNext window
Ctrl+a pPrevious window
Ctrl+a "List all windows for selection
Ctrl+a ARename the current window
Ctrl+a [Enter copy/scroll mode (use arrows, q to exit)
Ctrl+a kKill the current window (asks for confirmation)
Ctrl+a ?List all available shortcuts

The concept of “windows inside a session” is powerful: within a single deploy session you can have one window running the build, another with htop monitoring, another with tail -f on logs. All isolated, all persistent.

Scroll buffer is limited

By default, screen keeps only 100 lines of history in the buffer. If your application spits out a lot of log, consider redirecting output to a file with command 2>&1 | tee /tmp/output.log — or increase the buffer permanently by creating ~/.screenrc with defscrollback 10000.

Properly closing sessions

When the application finishes, you need to close the session so it doesn’t linger as a zombie in screen -ls.

01

Reattach the session and stop the process normally (Ctrl+C or the app’s exit command). Then close the session’s shell:

exit

This closes the shell that screen created. The session is destroyed and disappears from screen -ls. This is the clean way.

02

Kill a session by force without reattaching (use when it’s stuck):

screen -X -S deploy quit

The -X flag sends a command to a specific session, and quit terminates it. Useful when the process inside is stuck and you don’t want to reattach to debug.

03

Clean up dead sessions that still appear in screen -ls:

screen -wipe

This command removes all orphaned sockets from sessions whose process has already died. It runs fast and is safe — it doesn’t touch valid active or detached sessions.

Verification

To confirm everything works, do the real-world test: disconnect from SSH with an application running and reconnect later.

01

Create a test session with a simple counter:

screen -S test
for i in $(seq 1 600); do echo "Tick $i — $(date +%T)"; sleep 1; done
02

Detach with Ctrl+a d and close the SSH connection completely (exit or close the terminal).

03

Reconnect to the server via SSH and reattach:

screen -r test

The counter should be well past where you detached — proof it kept running without you. Stop it with Ctrl+C and exit.

Troubleshooting

”Cannot open your terminal ‘/dev/pts/0’”

This appears when you switch users (sudo su - other) and try to use screen. The new user doesn’t have permission on the original pty. Solve it by running script /dev/null before screen — this allocates a fresh pty for the current user.

Session shows “Attached” but you’re not in any

A previous SSH connection dropped without a clean detach. screen thinks it still has a client. Solve it with screen -d -r name — the -d flag forces a detach before attaching to you.

Broken colors and formatting inside screen

The $TERM variable inside screen becomes screen or screen.xterm-256color, which some applications don’t recognize. Add export TERM=xterm-256color to your .bashrc or run it before the application.

Don't run screen as root unless necessary

screen sessions running as root expose a persistent privileged shell that survives disconnections. If the session is hijacked (shared via -x, or unauthorized access to the root user), the attacker inherits root directly. Always prefer running as a regular user and elevate with sudo only when needed inside the session.

Next steps

Now that you’ve got the basics, here are some paths to go deeper:

  • Create a ~/.screenrc file with persistent settings (custom status bar, remapped shortcuts, large scrollback). Full examples in man screen.
  • Learn tmux as a modern alternative — similar syntax, but with native graphical splits and much cleaner file-based configuration.
  • For processes that need to survive reboots, set up a systemd service unit with Restart=always. screen and tmux don’t cover this case.
  • Study journalctl --user to centralize logs from long-running applications in the background.

If you’re running long-duration workloads — multi-hour builds, model training, data processing — a Hostini VPS with NVMe SSD and dedicated CPU cuts execution time and brings predictability, which pairs well with heavy use of screen to keep everything organized in sessions.

Frequently asked questions

What is the difference between screen, tmux, and nohup?

nohup only detaches the process from the terminal and redirects output to a file — you cannot reattach or interact later. screen and tmux keep the process inside a full session that you can detach, reattach, and manipulate. tmux is more modern (graphical splits, file-based configuration), while screen is simpler and is installed by default on almost any distribution.

If the server reboots, does the screen session survive?

No. screen keeps the session alive only while the parent process (the daemon) is running, and that does not survive a reboot. To survive restarts, use systemd with a service unit, or supervisord. screen is for long tasks within a single uptime — compilations, model training, maintenance scripts.

Why does my screen session show up as (Dead) in screen -ls?

It means the process inside the session ended but the socket was left orphaned. Clean it with `screen -wipe`, which removes all dead sessions. If you want to capture the output before killing it, try `screen -r name` anyway — in some cases you can still read the last lines from the buffer.

Can I share a screen session with another user?

Yes, using `screen -x session_name` both users can view and type simultaneously. Useful for remote pair-debugging. To share with another Linux user on the same server, the owner needs to run `screen -X multiuser on` and `screen -X acladd other_user` inside the session.

How do I view the full output of the application running inside screen?

Reattach the session with `screen -r name` and scroll up using Ctrl+a, followed by Esc. Then use the arrow keys or Page Up/Down to navigate through the buffer (about 100 lines by default). To increase the buffer, add `defscrollback 10000` to ~/.screenrc.

screen is consuming CPU even with nothing inside — is that normal?

No. Idle screen sessions consume essentially zero CPU. If you see high usage, it's probably the process inside the session that's consuming (an infinite loop, log spam, etc). Reattach with `screen -r` and investigate. If many dead sessions have piled up, run `screen -wipe`.

Topics:
Next steps Ryzen cloud with NVMe storage and always-on DDoS protection.Go live on a Hostini VPS →
Was this tutorial helpful?
Chat on WhatsApp