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

# Timeouts, pause and resume

> Keep a sandbox's files, processes and memory while you are away, set how long it lives, and know what is metered.

| You want | Use | Keeps | Lasts |
| - | - | - | - |
| to stop for a while and carry on in the same sandbox | [pause and resume](#pause-and-resume) | memory, processes, files | until you kill it |
| the same start for many later sandboxes | a [snapshot](#snapshots) | memory, processes, files | until you delete it |
| the files only, after the sandbox is gone | read them out first ([files](https://docs.styne.ai/files.md)) | files you copied | yours |

## Pause and resume

```python Python theme={"theme":"css-variables"}
from e2b import Sandbox

sbx = Sandbox.create(template="base", timeout=300)
sbx.files.write("/root/state.txt", "remembered\n")
sbx.pause()
again = Sandbox.connect(sbx.sandbox_id)
print(again.files.read("/root/state.txt").strip())
again.kill()
```

```typescript TypeScript theme={"theme":"css-variables"}
import { Sandbox } from 'e2b'

const sbx = await Sandbox.create('base', { timeoutMs: 300_000 })
await sbx.files.write('/root/state.txt', 'remembered\n')
await sbx.pause()
const again = await Sandbox.connect(sbx.sandboxId)
console.log((await again.files.read('/root/state.txt')).trim())
await again.kill()
```

```bash cURL theme={"theme":"css-variables"}
curl -sS -X POST https://api.styne.ai/sandboxes/$SANDBOX_ID/commands \
  -H "Authorization: Bearer $STYNE_API_KEY" -H "content-type: application/json" \
  -d '{"cmd": "sh", "args": ["-c", "echo remembered > /root/state.txt"]}'

curl -sS -X POST https://api.styne.ai/sandboxes/$SANDBOX_ID/pause \
  -H "Authorization: Bearer $STYNE_API_KEY" -H "content-type: application/json" \
  -d '{"tier": "memory"}'

curl -sS -X POST https://api.styne.ai/sandboxes/$SANDBOX_ID/resume \
  -H "Authorization: Bearer $STYNE_API_KEY" -H "content-type: application/json" \
  -d '{"timeout": 300}'

curl -sS -X POST https://api.styne.ai/sandboxes/$SANDBOX_ID/commands \
  -H "Authorization: Bearer $STYNE_API_KEY" -H "content-type: application/json" \
  -d '{"cmd": "cat", "args": ["/root/state.txt"]}'
```

In the cURL example the last answer holds `remembered` in `stdout`. A pause answers `204`. A resume answers `201` with the
sandbox as a create answer: the same `sandboxID`, the same state it had before the pause, and a new `timeout` (seconds, as
on create).

Pausing a paused sandbox, or resuming a running one, answers `409`. A command on a paused sandbox answers
`412 sandbox_paused`: resume it first. `POST /sandboxes/{id}/connect` returns the sandbox the same way and resumes it if it is
paused.

## Pause tiers

A pause keeps a sandbox's state so a resume is transparent: the process, its memory and its open files are where they
were. There are three tiers, and the tier decides how much is kept and how long a resume takes. Pick the tier by how long you
will be away.

| Tier | What is kept | Resume |
| - | - | - |
| `memory` (the default) | The whole guest memory, kept on the node | Transparent, the fastest |
| `standby` | A snapshot of memory and disk on the node's disk; the memory is freed | Transparent, a restore from the snapshot |
| `archived` | The standby snapshot, also sealed in object storage | Transparent, a restore from the snapshot |

A pause with no body is a `memory` pause; a `tier` other than these three is refused with `400 invalid_argument`.

## Timeouts

`timeout` on create is the number of seconds the sandbox lives, from the start: 1 to 86,400, and 300 when you leave it out
(a plan may allow less). While a sandbox is paused no timeout runs.

```bash theme={"theme":"css-variables"}
curl -sS -X POST https://api.styne.ai/sandboxes/$SANDBOX_ID/timeout \
  -H "Authorization: Bearer $STYNE_API_KEY" -H "content-type: application/json" \
  -d '{"timeout": 600}'

curl -sS -X POST https://api.styne.ai/sandboxes/$SANDBOX_ID/refreshes \
  -H "Authorization: Bearer $STYNE_API_KEY" -H "content-type: application/json" \
  -d '{"duration": 120}'
```

`/timeout` sets a new lifetime from now. `/refreshes` keeps the sandbox alive for at least that many more seconds and never
shortens it, so a keep-alive loop can call it freely. Both answer `204`. In the SDKs it is `sbx.set_timeout(600)` and
`sbx.setTimeout(600_000)`. A sandbox that reaches its timeout is killed and answers `410 sandbox_timeout`.

### Pause on timeout instead of kill (auto-sleep)

Create the sandbox with `"lifecycle": {"onTimeout": "pause", "autoResume": true}` (the E2B SDKs send the same fields) and a
sandbox that reaches its timeout is paused instead of killed; with `autoResume`, the next call to it wakes it. `autoResume`
needs `onTimeout: "pause"`, or the create answers `400`. `GET /sandboxes/{id}` shows the `state`. An account with
[`snapshots_off_on_stop`](https://docs.styne.ai/data-retention) set gets a kill at the timeout whatever the create asks.

### While it runs: processes and the clock keep going

A running sandbox keeps running its processes and its clock from create until its timeout, whether or not a command or a
request is in flight: a server or job you started with `nohup ... &` is still there, and still working, when you come back.
The platform puts a running sandbox to sleep only when nothing of yours runs inside it (no process started after the
sandbox was created: no server, job or open shell). Such a sandbox costs nothing to keep and wakes in milliseconds on the
next command, file call or connection, so there is nothing to notice.

To save money on a sandbox that does keep a process running, give it an idle sleep of your own on create:
`"idleTimeout": 120` (seconds, 0 to 3600; 0 or absent means none). The sandbox then sleeps once nothing has called or
connected to it for that long, even if a process is running, and wakes on the next call or connection; while it sleeps
the processes and the clock are stopped. A job that must keep running on its own needs no `idleTimeout`. A cron entry or
timer with no process alive between runs is not counted as work: keep a process in the sandbox (a scheduler loop is one).

## What is metered

* A running sandbox is metered per second until it is killed, reaches its timeout or is paused.
* A paused sandbox is metered at its tier's rate. A memory pause keeps its memory on the node and still counts against
  your concurrent sandboxes; a `standby` or `archived` sandbox does not, and a slot is free the moment a sandbox is
  killed or put to standby (see [limits](https://docs.styne.ai/limits.md)).
* A browser session is metered only while it is awake; a paused or sleeping browser accrues no charge.
* [Billing](https://docs.styne.ai/billing.md) says what is metered, and `GET /v1/run/usage` shows your own numbers.

> [!WARNING]
>
> A paused sandbox you forget still counts against your sandboxes held in any state: kill what you no longer need.

## Snapshots

A snapshot saves memory and disk under a name; any later sandbox of yours can start from it and begins exactly there. The
full set of calls, with listing, deleting and reading files inside a snapshot, is on [Snapshots](https://docs.styne.ai/snapshots.md).

```bash theme={"theme":"css-variables"}
curl -sS https://api.styne.ai/sandboxes/$SANDBOX_ID/snapshots \
  -H "Authorization: Bearer $STYNE_API_KEY" -H "content-type: application/json" \
  -d '{"name": "keep-state-demo"}'

curl -sS https://api.styne.ai/sandboxes \
  -H "Authorization: Bearer $STYNE_API_KEY" -H "content-type: application/json" \
  -d '{"snapshot": "keep-state-demo", "timeout": 120}'

curl -sS -X DELETE https://api.styne.ai/snapshots/keep-state-demo \
  -H "Authorization: Bearer $STYNE_API_KEY"
```

The second call answers `201` with a new `sandboxID` whose `/root/state.txt` is already there. A name is unique per
account (`409` when taken). Run `sync` in the sandbox before you save it when you want its files to be visible without
starting it.

## Operations

| Method | Path | What it does |
| - | - | - |
| `POST` | `/sandboxes/{id}/pause` | Pause a sandbox at a tier |
| `POST` | `/sandboxes/{id}/resume` | Resume a paused sandbox |
| `POST` | `/sandboxes/{id}/connect` | Return a sandbox and resume it if it is paused |
| `POST` | `/sandboxes/{id}/timeout` | Set a new lifetime for a running sandbox |
| `POST` | `/sandboxes/{id}/refreshes` | Extend the lifetime of a running sandbox |


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