Run Claude Code in tmux So an SSH Drop Does Not Orphan It

aidockerself-hosting
Run Claude Code in tmux So an SSH Drop Does Not Orphan It

Start Claude Code inside tmux, a program that keeps your terminal running on the server after SSH drops. Without it, a dropped connection ended the host side of devcontainer exec and left claude running in the container with no terminal to reattach to. With tmux, you log back in and run tmux attach to get the same screen.

I tested this on November 20, 2026: a 2 vCPU / 8 GB Ubuntu 24.04 VPS with Docker 29.10, tmux 3.4, and Claude Code 2.1.329. Claude Code ran in a dev container (a Docker container defined by a .devcontainer folder in the repo), started with the Dev Containers CLI.

What Happens When SSH Drops Without tmux?

I started Claude Code over SSH with devcontainer exec, killed the SSH client to simulate a dropped connection, and listed the processes on the host and in the container:

Terminal output of three process lists. While connected, the host runs devcontainer exec and docker exec, and the container runs claude. Ten seconds after the SSH client was killed, the host list is empty but claude is still running in the container. Two minutes later, claude is still there.

The host side was gone within 10 seconds. claude was still running in the container two minutes later, with nothing connected to its terminal. It's an orphan: a process that keeps running with nothing attached to it.

You can't get back to it, because docker exec always starts a new process and can't attach to a running one. Killing a tmux session that holds Claude Code left an orphan too.

How Do You Keep Claude Code Running With tmux?

tmux runs a small server process on the host. Your terminal lives inside that server, so it stays when SSH drops.

Two panels. Without tmux, an SSH session on the host starts devcontainer exec, which starts docker exec, which starts claude inside the container. When SSH drops, the host processes die and claude is left orphaned in the container with no terminal. With tmux, the tmux server on the host holds the terminal for devcontainer exec, so SSH can drop and you reattach to the same claude session.

Start a named session on the server, then start Claude Code inside it:

tmux new -s claude
cd ~/projects/orders-api
devcontainer exec --workspace-folder . claude

Press Ctrl+b, then d, to detach. The session keeps running, and you can close SSH.

You can also pass the command to tmux, so one line starts both:

Terminal output on the host. One tmux new command starts devcontainer exec with claude inside the session. After Ctrl+b and d it prints detached from session claude, and tmux ls still lists the claude session.

If that command fails, the tmux session closes and takes the error with it. Run the part in quotes on its own to see what went wrong.

When you log back in, attach to it:

tmux attach -t claude

I ended the SSH session and logged back in 50 seconds later. The session was still listed, and its screen showed Claude Code where I left it:

Terminal output after logging back in. tmux ls lists the claude-test session created at 10:41:36, and tmux capture-pane prints the live Claude Code screen, still on its theme picker with a code diff preview.

capture-pane -p prints a session's screen without attaching to it.

Which tmux Commands Do You Need?

These six cover everything in this guide:

CommandWhat it does
tmux new -s claudeStarts a session named claude and attaches to it
Ctrl+b, then dDetaches. The session keeps running
tmux attach -t claudeReattaches with the screen as you left it
tmux lsLists sessions
tmux capture-pane -p -t claudePrints the screen without attaching
tmux kill-session -t claudeEnds the session. Quit Claude Code first

tmux 3.4 came with the test server's Ubuntu 24.04 image. If yours doesn't have it, run sudo apt install tmux.

How Do You Find and Stop an Orphaned Claude Process?

Find the container the Dev Containers CLI started for your repo. Run this from the repo folder on the host, because the label holds that folder's full path:

docker ps -q --filter label=devcontainer.local_folder=$PWD

Then list the processes in that container with their start times:

docker exec <container> ps -eo pid,lstart,tty,args

Every Claude Code session shows up as claude, so check the start time before you kill anything. In my test, an orphan from the first SSH test sat next to a live tmux session:

Terminal output of ps inside the dev container, trimmed to two claude processes. PID 10875, started at 10:41:04, is the orphan from the SSH test. PID 10956, started at 10:41:36, is the live session in tmux.

Stop only the process you own:

docker exec <container> kill <pid>

pkill -x claude also works, but it stops every claude in the container, live sessions included.

Should tmux Run on the Host or in the Container?

Run tmux on the host and start devcontainer exec inside it. That is the setup I tested. The dev container image doesn't include tmux, so I left that option out.

The Remote Control docs give the same advice for a remote machine: start Claude Code inside tmux or screen so the session survives an SSH disconnect. Remote Control and long runs without permission prompts both depend on it.

tmux does not survive a reboot. I didn't test reboots, systemd services, mosh, or a network that stops delivering packets without closing the connection.

Summary

Start Claude Code inside tmux on the host, detach with Ctrl+b and d, and close SSH. tmux attach -t claude brings back the same screen. Without tmux, a dropped connection leaves claude orphaned in the container, so find it by start time and kill only that PID.

Thanks for reading.

And stay awesome!


Frequently Asked Questions

How do I keep Claude Code running after SSH disconnects?

Start it inside a tmux session on the server. When SSH drops, tmux keeps the terminal and Claude Code running. After you log back in, run tmux attach to get the live screen back.

What happens to Claude Code when SSH disconnects without tmux?

In testing with a dev container, the host side of devcontainer exec was gone within 10 seconds, but claude kept running inside the container with no terminal. It was still there two minutes later, and there was no way to reattach to it.

How do I stop an orphaned Claude Code process in a dev container?

List the processes in the container with docker exec and ps, check the start time to find the one you started, and kill that process ID with docker exec. Avoid killing every claude process, because another session on the server can have the same name.

Should I run tmux on the host or inside the dev container?

Run it on the host and start devcontainer exec inside it. That is the setup I tested, and it kept Claude Code alive and reachable. Running tmux inside the container was not tested.

Does tmux keep Claude Code running after a reboot?

No. A reboot ends tmux sessions and everything running in them. Reboots were not part of this test, so plan to start the dev container and Claude Code again after one.

Browse all Self-Hosting guides →
Loading comments...

Whenever you're ready, there are 5 ways I can help you:

  1. Pragmatic Self-Hosting: Learn to deploy and run your apps on a VPS with Dokploy and Tailscale. Keep administration private, automate releases, and practice restoring backups, with source code and a runbook you can reuse.
  2. Pragmatic Clean Architecture: Join 5,000+ students in this comprehensive course that will teach you the system I use to ship production-ready applications using Clean Architecture. Learn how to apply the best practices of modern software architecture.
  3. Modular Monolith Architecture: Join 2,800+ engineers in this in-depth course that will transform the way you build modern systems. You will learn the best practices for applying the Modular Monolith architecture in a real-world scenario.
  4. Pragmatic REST APIs: Join 1,900+ students in this course that will teach you how to build production-ready REST APIs using the latest ASP.NET Core features and best practices. It includes a fully functional UI application that we'll integrate with the REST API.
  5. Patreon Community: Join a community of 5,000+ engineers and software architects. You will also unlock access to the source code I use in my YouTube videos, early access to future videos, and exclusive discounts for my courses.

The .NET Weekly

One practical .NET tip, every Saturday.

Join 66,000+ engineers who read it. Free, and you can unsubscribe any time.