Skip to main content

Persistent SSH Sessions Using the screen Command

Did your SSH connection drop mid-command? Learn how to keep long-running WP-CLI tasks and background processes safely running on Seravo using the screen utility.

If your network connection drops during a long-running command over SSH, the active process is normally terminated immediately. With the screen utility (GNU Screen), you can create a virtual terminal session that continues running in the background on the server even if you close your terminal window or lose your connection temporarily.

screen is particularly useful for resource-intensive WordPress maintenance tasks, such as:

  • Regenerating thousands of thumbnails (wp media regenerate)

  • Importing or exporting large databases (wp db import / wp db export)

  • Running extensive database search-and-replace operations (wp search-replace)

Note: screen is intended for one-off command-line tasks. If you need to run persistent background services (such as Node.js applications), use a process manager like PM2 and crontab, as screen sessions do not automatically restart after server maintenance windows or reboots.

Prerequisites

Before getting started, ensure you have:

Step-by-Step Instructions

1. Start a New screen Session

Once connected to your server via SSH, start a new screen session with:

screen

Tip: If you are running multiple tasks concurrently, assign a descriptive name to the session using the -S flag—for example, screen -S media-regenerate.

A new virtual terminal session opens, behaving just like a standard command prompt.

2. Run Your Commands

Start your long-running WP-CLI command or script inside the screen session as usual, for example:

wp media regenerate --only-missing

3. Detach from the Session

To leave the screen session while keeping your process running in the background, press Ctrl + A, release both keys, and then press D (Detach).

You will return to your standard SSH shell with the message [detached from ...]. You can now safely close your SSH connection while the process continues executing on the server.

4. List Active Sessions

If your connection dropped unexpectedly or you want to check running background sessions, reconnect to the server via SSH and run:

screen -ls

Example terminal output:

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

5. Reattach to a Session

To return to a background session, run:

screen -r

If multiple sessions are active, specify the session ID or name:

screen -r 12345
# or using a named session:
screen -r media-regenerate

6. Close the screen Session When Finished

Once your task has completed and you are inside the screen session, terminate the session cleanly by typing exit or pressing Ctrl + D. The terminal will confirm with [screen is terminating].

Useful Keyboard Shortcuts Inside a Session

All screen shortcuts start by pressing Ctrl + A, releasing, and then pressing the action key:

Shortcut

Action

Ctrl + A, then D

Detaches the session to the background while keeping the process running.

Ctrl + A, then [

Enters scrollback (Copy) mode so you can scroll up and down using the arrow keys or Page Up / Page Down. Press Esc to exit.

Ctrl + A, then H

Logs all session output to a screenlog.0 file in the current directory.

Ctrl + A, then ?

Displays a full list of screen keybindings.

Common Issues and Troubleshooting

  • Error There is no screen to be resumed: Check screen -ls to see whether the session is still active. If the command inside the session already finished or you closed it with exit, the session no longer exists.

  • Session status is (Attached) and screen -r refuses to connect: If your connection dropped abruptly, the server may still consider the previous SSH session attached. Force-detach the stale connection and reattach in a single command:

    screen -d -r
  • The session list displays (Dead ???): If a session terminated abnormally, clean up dead sessions from the list with screen -wipe.

  • Mouse wheel scrolling does not scroll backward inside screen: screen captures native terminal scrollback. Enter copy mode by pressing Ctrl + A followed by [, scroll using the arrow keys, and press Esc to return to normal mode.

See Also

Using screen ensures that long WP-CLI operations and maintenance tasks complete reliably even over unstable network connections. If you need assistance establishing an SSH connection or running resource-intensive command-line tasks, our support team is happy to help at [email protected].

Did this answer your question?