Skip to content

Use screen to safely run long-running deployment tasks in the background. ​

Diagram of using screen to safely run long-duration deployment tasks in the background

Scene:ceph-cluster deploy -v Deployment commands like this take a long time. I want to close SSH and leave work, then come back the next day to check the progress. This article documents the pitfalls I encountered and the final solution.

Problem Background ​

Execute the cluster deployment command:

bash
ceph-cluster deploy -v

This command takes a very long time. To get off work on time, the intuitive approach is to use & Put it in the background:

bash
ceph-cluster deploy -v &

Then turn off SSH.In the end, when I came back the next day, I found that the process had long since disappeared.

Pitfall 1: Only use &, SSH disconnection will kill the process ​

& Merely putting the process in the background; it is still a child process of the current shell session. When SSH disconnects, the system will send to all child processes under that session SIGHUP Signal, the default behavior of the process is to exit.

So to survive detachment from SSH, you must make the process ignore SIGHUP, or leave the current session.

Pitfall 2:nohup It fails outright when it encounters an interactive confirmation. ​

Use nohup Can be ignored SIGHUP:

bash
nohup ceph-cluster deploy -v > deploy.log 2>&1 &

But after executing this command, an interactive prompt popped up:

text
? Confirm to deploy cluster? (y/N)

nohup will disconnect standard input (stdin). The program reads this y When reading, it is EOF; many programs by default treat it as N(No), therefore Exit directly, the deployment hasn't started at all.

Check the help to confirm that there is no parameter for skipping confirmation:

bash
ceph-cluster deploy --help

It's not in the output.-y / --yes/--non-interactive options like that—this interaction can't be avoided.

坑三:Ctrl+Z→bg→disown It can be salvaged, but the risk is high. ​

It is already running in the foreground, input y After that, you can use this combo to switch to the background:

bash
# 1. Enter y and press Enter, then wait for it to start scrolling logs.
# 2. Press Ctrl+Z to suspend
bg        # Let suspended tasks continue in the background
disown    # Removed from the current shell job table; SSH disconnection is not affected.

But this plan has two fatal problems:

  • disown After that, it can no longer be used.fg Bring it back to the foreground; if it gets stuck on an interaction prompt midway, you won't be able to see it or answer it.

  • can only be rescued once Interaction—if there are subsequent confirmation steps, it will still fail.

Pitfall 4: The machine has no external network access, so tmux cannot be installed. ​

Want to pretend tmux Once and for all:

bash
tmux -V || yum install -y tmux

As a result, yum threw an error, and even DNS couldn't be resolved:

text
Could not resolve host: mirrorlist.centos.org; Unknown error
Cannot find a valid baseurl for repo: base/7/x86_64

This CentOS 7 machine has no internet access, so yum can't install anything.

Final solution: use the system's built-in screen. ​

Check if there is any screen:

bash
screen -v

Output:

text
Screen version 4.01.00devel (GNU) 2-May-06

Built-in, perfect.screen It will create a pseudo-terminal independent of the SSH session; interaction works completely normally, and disconnecting SSH does not affect it.

Operating steps ​

1. Create a new screen session

bash
screen -S deploy

2. Run the deployment command normally in screen

bash
ceph-cluster deploy -v

Enter normally when you see the interactive prompt:

text
? Confirm to deploy cluster? (y/N)  →  y

Make it start spamming the logs.

3. After confirming it is running, detach from the session.

text
Ctrl + A   Release
Press D again

Press the two actions separately, not at the same time. After success, it will return to the normal shell, with a prompt similar to:

text
[detached from 12345.deploy]

Now the deployment process runs independently in screen,Turn off SSH, clock out and leave—completely unaffected..

4. Come back the next day to check progress.

bash
screen -ls          # List all screen sessions
screen -r deploy    # attach back to the deploy session

After entering, you can see the complete interface and current progress. If the deployment has ended, after viewing the results, enter exit Exit.

Frequently Asked Questions ​

screen -r deploy Hint Attached

This indicates that this session is still connected elsewhere. Detach first, then attach:

bash
screen -d deploy
screen -r deploy

screen -ls Display Dead

The session has ended; just clear the records:

bash
screen -wipe

Solution comparison ​

PlanCan it handle interaction?SSH Disconnect KeepaliveIs installation required?Applicable scenarios
&No❌NoVery short task
nohup ... &❌✅NoNon-interactive long-running tasks
Ctrl+Z+bg+disownOnly one rescue attempt is allowed.✅NoEmergency, no subsequent interaction.
tmux✅✅Installation requiredAn ideal solution, but it can't be installed in restricted environments.
screen✅✅System built-inThis article recommends

Summary ​

  • Long-duration deployment task Don't Only use &, it's gone as soon as SSH disconnects.

  • nohup Can prevent SIGHUP, but it will exit directly when encountering interactive confirmation because stdin is disconnected.

  • Can't install without internet access.tmux when,system-provided screen is the best choice.

  • Key memory point:Ctrl+A Then D is detach,screen -r <name> It is recovery.

One command to keep you safe:

bash
screen -S deploy

Done and wrapping up—time to clock out.

Last updated on11/07/2026, 02:16 PM
@fishfinal | @fishfinals | [email protected]