# Long-running sessions with tmux

> Keep coding agents running in a flockvm VM after you disconnect: tmux basics, logging, headless runs, and what survives a pause and resume.

- URL: https://flockvm.com/docs/agents/tmux
- Section: Coding agents
- Last updated: 2026-11-01

An agent started straight from your SSH session dies when the connection drops. Start it inside tmux instead: the session lives in the VM, and you attach and detach as you like. tmux is already installed in every flockvm VM, on both images. The commands below log in as `user`, the login of the Agent image; on a Minimal VM, use the user you run your agent as (see [Run Claude Code in a VM](https://flockvm.com/docs/agents/claude-code#on-the-minimal-image)).

## tmux in one minute

| You want to                                    | Run or press                                                |
| ---------------------------------------------- | ----------------------------------------------------------- |
| Start a named session                          | `tmux new -s claude`                                        |
| Start one in the background, running a command | `tmux new -d -s task 'npm test'`                            |
| Detach, leaving it running                     | `Ctrl-b` then `d`                                           |
| List sessions                                  | `tmux ls`                                                   |
| Attach again                                   | `tmux attach -t claude`                                     |
| Open a new window in the session               | `Ctrl-b` then `c`                                           |
| Switch window                                  | `Ctrl-b` then the window number, or `Ctrl-b` `w` for a list |
| Scroll back                                    | `Ctrl-b` then `[`, arrows or `PgUp`, `q` to leave           |
| End a session                                  | `tmux kill-session -t task`                                 |

Claude Code also uses `Ctrl-b`: inside tmux, press it twice to send it to Claude Code.

## Survive a dropped connection

1. **SSH in and start a session**

   ```bash
   ssh -p 22001 user@HOST
   tmux new -s claude
   ```

2. **Start the agent inside it**

   ```bash
   cd ~/your-repo
   claude --dangerously-skip-permissions
   ```

3. **Detach and leave**

   Press `Ctrl-b` then `d`, and close your terminal, or let Wi-Fi drop. The agent keeps working in the VM.

4. **Come back**

   Attach straight from your laptop in one command:

   ```bash
   ssh -t -p 22001 user@HOST tmux attach -t claude
   ```

   `-t` gives tmux the terminal it needs. `tmux new -A -s claude` attaches to the session if it exists and creates it otherwise.

To keep idle SSH connections from being closed by routers along the way, add keepalives to your SSH config:

```ssh-config title="~/.ssh/config"
Host HOST
  ServerAliveInterval 30
  ServerAliveCountMax 4
```

> **Warning: mosh does not work.** Each VM accepts only one inbound port from the Internet: its SSH port, which forwards to the VM's port 22. mosh needs inbound UDP, which never reaches the VM. Use SSH with tmux and keepalives instead.

## Log what the agent does

tmux keeps a scrollback, not a file. To keep a full record, write one.

**tee:**

Pipe the command through `tee` when you start it. Works best for non-interactive runs:

```bash
tmux new -d -s task 'cd ~/your-repo && npm test 2>&1 | tee ~/test.log'
tail -f ~/test.log
```

**pipe-pane:**

Record everything a pane shows, for an interactive session already running. Run it inside the session, or from outside with `-t`:

```bash
tmux pipe-pane -o -t claude 'cat >> ~/claude-session.log'
```

Run the same command again to stop. The log contains the terminal's escape codes; read it with `less -R`.

**capture-pane:**

Save what is in a pane's scrollback right now, as plain text:

```bash
tmux capture-pane -p -S - -t claude > ~/claude-scrollback.txt
```

Copy logs back to your laptop with `scp -P 22001 user@HOST:claude-session.log .`.

## Run Claude Code headless

`claude -p` runs one prompt without the interactive UI, prints the result and exits. It skips the folder trust dialog, so it works from a script:

```bash title="As user, inside the VM"
cd ~/your-repo
tmux new -d -s nightly 'claude -p --dangerously-skip-permissions "Update the dependencies, fix what breaks, run the tests, and commit" 2>&1 | tee ~/nightly.log'
```

Check whether it is still running:

```bash
tmux has-session -t nightly 2>/dev/null && echo running || echo done
```

When the command ends, its tmux session closes; the log stays. To keep the window open after the command ends, so you can read the end of it in tmux, append `; exec bash` to the command. The Codex equivalent is `codex exec`: see [Run Codex CLI in a VM](https://flockvm.com/docs/agents/codex#run-a-one-shot-task).

## Several agents in one VM

One VM can hold several tmux windows, one agent per window, each in its own git worktree:

```bash title="As user, inside the VM"
cd ~/your-repo
git worktree add -b task-a ../task-a
git worktree add -b task-b ../task-b
tmux new -d -s agents -n a -c ~/task-a
tmux new-window -t agents -n b -c ~/task-b
tmux send-keys -t agents:a 'claude --dangerously-skip-permissions' Enter
tmux send-keys -t agents:b 'claude --dangerously-skip-permissions' Enter
tmux attach -t agents
```

Each new worktree asks once whether you trust the folder: attach and answer in each window. The agents still share the VM's CPU, RAM, ports and database. When they start servers or run migrations, use [one VM per agent](https://flockvm.com/docs/agents/parallel-agents). The tmux settings for Shift+Enter, notifications and spotting a waiting agent are in [Claude Code with tmux](https://flockvm.com/learn/claude-code-tmux).

## What happens on pause and resume

Pausing a VM is the cheap way to leave an agent session overnight.

| Action     | Your tmux sessions and agents                    | Disk    | Billing      |
| ---------- | ------------------------------------------------ | ------- | ------------ |
| **Pause**  | Frozen: memory and processes are written to disk | Kept    | Stops        |
| **Resume** | Back where they were: same processes, same PIDs  | Kept    | Starts again |
| **Stop**   | Gone: the VM shuts down cleanly                  | Kept    | Stops        |
| **Start**  | A fresh boot: start your sessions again          | Kept    | Starts again |
| **Delete** | Gone                                             | Deleted | Stops        |

On **Resume**, tmux and everything in it continue where they were. Open TCP connections survive the pause, so an SSH session attached at the moment of the pause can come back as well. The VM's clock is set to the current time on resume.

What can still fail: services outside the VM did not pause. A request your agent had in flight to a model API or a Git host may have timed out on the other side, and the agent sees an error. Pause between tasks, when the agent is idle or waiting for you, rather than in the middle of a long request.

![The VM actions: Pause, Stop, Delete](https://flockvm.com/docs/screenshots/vm-actions.webp "Pause keeps memory and processes. Stop keeps only the disk.")

> **Tip: Before you stop a VM.** **Stop** ends every process, tmux included. If you want the session back as it was, **Pause** instead. Paused and stopped VMs are not billed for now.

## See a dev server from your laptop

Only the SSH port is open, so reach a server the agent started inside the VM through an SSH tunnel:

```bash title="On your laptop"
ssh -N -L 3000:localhost:3000 -p 22001 user@HOST
```

Then open `http://localhost:3000` in your browser. Stop the tunnel with `Ctrl-c`.

## Next steps

- [Run Claude Code in a VM](https://flockvm.com/docs/agents/claude-code): Install, sign in and run without prompts.
- [Parallel agents](https://flockvm.com/docs/agents/parallel-agents): One VM per agent, fanned out over SSH.
- [Lifecycle](https://flockvm.com/docs/vms/lifecycle): Start, stop, pause, resume and delete, in detail.
- [Networking](https://flockvm.com/docs/vms/networking): The SSH port, outbound traffic and DNS.
