# Sleep and wake

A sandbox that is not doing anything can sleep: kosa8 saves its memory and disk and frees the machine. The next call wakes it where it left off, running processes included.

**CLI**

```
ID=$(kosa8 cloud sandbox create --template python --ttl 30m --json | jq -r .sandbox.id)
kosa8 cloud sandbox exec $ID -- sh -c 'echo "still here" > /workspace/note'

kosa8 cloud sandbox sleep $ID                 # snapshot it and free its memory
kosa8 cloud sandbox get $ID --json | jq -r .sandbox.state     # sleeping

kosa8 cloud sandbox exec $ID -- cat /workspace/note   # the next exec wakes it
kosa8 cloud sandbox rm $ID
```

**curl**

```
API=https://api.kosa8.com/v1
AUTH="Authorization: Bearer $KOSA8_API_KEY"
JSON='Content-Type: application/json'
SBX=$(curl -s $API/sandboxes -H "$AUTH" -H "$JSON" -d '{"template":"python","ttl_s":1800}' | jq -r .sandbox.id)
curl -s $API/sandboxes/$SBX/exec -H "$AUTH" -H "$JSON" \
  -d '{"cmd":["sh","-c","echo still here > /workspace/note"]}' > /dev/null

curl -s -X POST $API/sandboxes/$SBX/sleep -H "$AUTH" | jq -r .sandbox.state   # sleeping

# Any exec wakes it first (or POST /wake to wake it now)
curl -s $API/sandboxes/$SBX/exec -H "$AUTH" -H "$JSON" -d '{"cmd":["cat","/workspace/note"]}' | jq -r .stdout

curl -s -X DELETE $API/sandboxes/$SBX -H "$AUTH"
```

**Python**

```
from kosa8 import Kosa8Cloud

cloud = Kosa8Cloud()

with cloud.sandboxes.create(template="python", ttl_s=1800) as box:
    box.exec("echo 'still here' > /workspace/note")
    box.sleep()                                    # snapshot it and free its memory
    print(box.state)                               # sleeping
    print(box.exec("cat /workspace/note").stdout)  # wakes it first: still here
```

**JavaScript**

```
import { Kosa8Cloud } from '@kosa8/sdk';

const cloud = new Kosa8Cloud();
const box = await cloud.sandboxes.create({ template: 'python', ttlS: 1800 });
try {
  await box.exec("echo 'still here' > /workspace/note");
  await box.sleep();                                              // snapshot it, free its memory
  console.log(box.state);                                         // sleeping
  console.log((await box.exec('cat /workspace/note')).stdout);   // wakes it first: still here
} finally {
  await box.delete();
}
```

## How it works

- Set `sleep_after_s` on create (0 means never, up to 7 days) and an idle sandbox sleeps on its own. Or call `POST /v1/sandboxes/{id}/sleep`.

- A sleeping sandbox has state `sleeping`. Exec, file calls, checkpoints and snapshots wake it first; their answers carry `woke_ms`, how long that took. `POST /v1/sandboxes/{id}/wake` wakes it now.

- A sleeping sandbox does not run, so it does not use your plan's sandbox-hours. Its `ttl_s` deadline still counts.

- A [preview URL](https://docs.kosa8.com/guides/cloud-previews/) can wake its sandbox on the first request: [wake on request, and schedules](https://docs.kosa8.com/guides/cloud-wake-cron/).

---
Source: https://docs.kosa8.com/concepts/sleep/
