> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bonya.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# A persistent session you can walk away from and come back to

> Start a long-running process in a managed session, disconnect on purpose, and reattach later — even after the sandbox has suspended and resumed

Start a long-running process in a managed session, disconnect from it on purpose, and reattach later — even after the sandbox has suspended and resumed. This is the one thing plain `exec` can't do: a managed session outlives your connection (see [Sessions](https://docs.bonya.ai/concepts/sessions.md)). Every command below uses the `bonya-dev` template; if you haven't installed the CLI or logged in yet, start with the [Quickstart](https://docs.bonya.ai/quickstart.md).

**1. Create a sandbox**

```shell theme={null}
bonya create worker --template bonya-dev
```

**2. Start a managed session**

A simple loop stands in for any long-running process — a dev server, a queue worker, anything you'd otherwise leave running in a terminal you don't want to keep open.

```shell theme={null}
bonya session create worker watcher -- sh -c 'i=0; while true; do i=$((i+1)); echo "tick $i"; sleep 5; done'
```

**3. Attach, watch it, then detach without killing it**

```shell theme={null}
bonya session attach worker watcher
```

Ctrl+D (or Ctrl+C once) detaches your terminal — it does not kill the session, unlike a plain `exec`.

**4. Confirm it's still running from a fresh terminal**

```shell theme={null}
bonya session list worker
```

`ATTACHED` is now `no` — the session kept running after you detached.

**5. What suspend and resume look like**

A sandbox suspends automatically after enough inactivity, and resuming is implicit the next time you reach it (see [Sandboxes](https://docs.bonya.ai/concepts/sandboxes.md)). This particular session won't trigger that on its own — a detached session's own output still counts as activity and defers idle suspend, so a `tick`-every-5-seconds loop like `watcher` keeps the sandbox awake by design (see [Sessions](https://docs.bonya.ai/concepts/sessions.md)). A quieter detached session (or the sandbox sitting untouched entirely) would suspend on its normal schedule instead.

Either way, listing sessions on a suspended sandbox doesn't wake it — you're served from a snapshot taken the moment it suspended, and the output says so explicitly:

```shell theme={null}
bonya session list worker
```

**6. Reattach**

Attaching to a suspended sandbox's session wakes it — the process was never stopped, it just wasn't being watched.

```shell theme={null}
bonya session attach worker watcher
```

The counter kept climbing the whole time — the same session, not a new one.

**7. Kill the session**

```shell theme={null}
bonya session kill worker watcher
```

**8. Clean up**

```shell theme={null}
bonya delete worker --force
```

## Next steps

> [card] [Sessions](https://docs.bonya.ai/concepts/sessions.md)
>
> The full attach/detach/replay model, and how the 1 MiB replay ring works.

> [card] [Sandboxes](https://docs.bonya.ai/concepts/sandboxes.md)
>
> Lifecycle states, and exactly when suspend and resume happen.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.