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:
SSH access to your site's server (see guide: How Do I Connect to My WordPress Site via SSH/SFTP?)
Basic familiarity with the command line
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 |
| Detaches the session to the background while keeping the process running. |
| Enters scrollback (Copy) mode so you can scroll up and down using the arrow keys or |
| Logs all session output to a |
| Displays a full list of |
Common Issues and Troubleshooting
Error
There is no screen to be resumed: Checkscreen -lsto see whether the session is still active. If the command inside the session already finished or you closed it withexit, the session no longer exists.Session status is
(Attached)andscreen -rrefuses 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 withscreen -wipe.Mouse wheel scrolling does not scroll backward inside
screen:screencaptures native terminal scrollback. Enter copy mode by pressingCtrl + Afollowed by[, scroll using the arrow keys, and pressEscto 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].
