# Attaching and Sessions
source: https://docs.chalk.ai/docs/sandboxes/attaching-and-sessions

## Reconnect to a running Chalk sandbox from another process, and run work that outlasts a single session.

### Attaching to an existing sandbox

Use Sandbox.from_id or Sandbox.from_name to reconnect to a sandbox from
another process, instead of creating a second one:

```
from chalkcompute import Sandbox

sandbox = Sandbox.from_id("sandbox-id")
# or, if it was created with a name=:
sandbox = Sandbox.from_name("hello-sandbox")
```

Sandbox.list_all() returns SandboxInfo for every sandbox in the current
environment, useful for finding an id or name to attach to.

### Running work across sessions

These examples use the multi-day-training sandbox from
Long-running sandboxes.

Sandbox.exec blocks until the command finishes, so start work measured in hours
or days as a detached process:

```
from chalkcompute import Sandbox

sandbox = Sandbox.from_name("multi-day-training")
sandbox.exec(
    "sh",
    "-c",
    "nohup python /app/train.py --checkpoint-dir /checkpoints > /checkpoints/train.log 2>&1 &",
)
```

Reattach by name from another process, or on another day, to check on it:

```
from chalkcompute import Sandbox

sandbox = Sandbox.from_name("multi-day-training")
print(sandbox.refresh().status)
print(sandbox.exec("tail", "-n", "20", "/checkpoints/train.log").stdout_text)
```

Within a single exec call, auto_reattach (enabled by default) reconnects and
replays buffered output after a transient transport disconnect, so brief network
interruptions need no handling of their own.

Terminate the sandbox once the work is done. A sandbox reserves the CPU, memory, and any
GPU you requested for as long as it exists, and those resources are billed as
provisioned rather than as used, so an idle sandbox costs the same as a busy one.
RestartPolicy.ALWAYS keeps bringing it back until you terminate it:

```
sandbox.terminate()
```





