diff --git a/bin/rclaude b/bin/rclaude index 5bc6277..06608c4 100755 --- a/bin/rclaude +++ b/bin/rclaude @@ -1603,9 +1603,17 @@ inner=$(build_inner "$dir") # background and print the session name. Symmetric with the local-host # detached branch above; used by supervisor processes (e.g. clare web) # that bring up a remote Claude pane without attaching the calling tty. +# +# We wrap `tmux new-session -d` in `systemd-run --user --scope --collect` +# so the tmux server is parented to its own systemd scope, not the ssh +# login session's scope. Without this, systemd-logind reaps the tmux +# server when the ssh session ends — even with `loginctl enable-linger`, +# transient ssh-spawn scopes get cleaned up after disconnect. The +# `--collect` flag lets systemd garbage-collect the scope after exit. # Mosh is interactive-only — always go through ssh for detached spawn. if [ -n "${RCLAUDE_DETACHED:-}" ]; then - ssh $_SSH_LIVE_OPTS "$host" "tmux new-session -d -s '${session}' \"${inner}\"" + _remote_cmd="systemd-run --user --scope --collect --quiet tmux new-session -d -s '${session}' \"${inner}\" 2>/dev/null || tmux new-session -d -s '${session}' \"${inner}\"" + ssh $_SSH_LIVE_OPTS "$host" "$_remote_cmd" printf '%s\n' "$session" exit 0 fi