A backgrounded daemon in a script sent to ssh host bash -s keeps the ssh session open
Symptom#
A script piped to ssh host bash -s starts a service in the background and then exits 0, but the local ssh never returns. The caller waits forever on a step that already printed success. On the remote host an extra bash -s... process stays alive as the parent of the service, with fd 1 and 2 on pipes:
3283069 1 bash -seuo pipefail
3283071 3283069 ./dc_zsvc <bind address>
lr-x------ 0 -> /dev/null
l-wx------ 1 -> pipe:[153181980]
l-wx------ 2 -> pipe:[153181981]
Cause#
For cmd >log 2>&1 </dev/null &, bash forks a subshell for the background job, and the redirections apply to cmd only. The forked subshell keeps the script's original stdout and stderr, which are the ssh session's pipes. It waits for cmd, so ssh never sees EOF. setsid nohup does not help, because the subshell that holds the pipes is not the process they act on.
Fix#
Put the redirection on a group, so the forked subshell gets it too:
&& { & }
To unstick a session that is already hung, kill the leftover bash -s parent on the remote host. The service keeps running, since setsid put it in its own session.
Evidence#
Seen in the dpucrumbs harness on 2026-09-29, where a provision step started dc_zsvc on a BlueField DPU and the run stalled 20 minutes before any cell ran. A test with { setsid nohup sleep 30 & } >/tmp/zt.log 2>&1 </dev/null over the same ssh path returned at once, and sleep 30 stayed alive on the host.
Also found by searching
- ssh never returns after the remote script printed success
- a daemon started in the background over ssh hangs the session
- how do I start a background service over ssh bash -s and return
- setsid nohup inside a remote script still blocks ssh
- ssh host bash -s < deploy.sh hangs forever after script prints done and exits 0
- my remote script starts a background service and the ssh command never returns
- how do I start a daemon over ssh with a piped script without the session hanging
- nohup cmd > log 2>&1 & over ssh stdin script still keeps pipe open, need setsid or close fds