---
title: Run processes in a sandbox
description: Start programs in a sandbox that run on after the call that started them, come back to them by name, read their output and send them input and signals.
related:
  - /files-and-code/run-a-sandbox
  - /files-and-code/connect-to-a-sandbox
  - /sdk/sandbox
---

`exec` waits for a command to end, and a call cannot wait for a dev server or a long build. Start those as processes: they run on after the call, and a later call comes back to them by name.

## Start a process

`spawn` starts a process and returns at once. Give it a `name` to find it again.

```ts luna/lunas/user.ts
startDev: h({}, async (ctx) => {
  const p = await ctx.sandboxes.box.spawn("npm run dev", { name: "dev", cwd: "/home/app", keepAwake: true });
  return p.name;
}),
```

A string runs with `/bin/sh -c`, an array runs as it is, and no command starts a login shell. The sandbox is declared as in [Run a sandbox](/files-and-code/run-a-sandbox).

| Option | Does |
| --- | --- |
| `name` | A name to come back to it by. Letters, digits, `-`, `_` and `.`, at most 64 |
| `pty` | Runs it on a terminal, 80 by 24 for `true` or `{ cols, rows }`. Its output and errors then come together |
| `cwd` | The working directory. `/home` by default |
| `env` | Environment variables to add |
| `user` | A user of the image to run as. `root` by default |
| `keepAwake` | Keeps the sandbox, and its luna, awake while it runs |
| `buffer` | How much of each output is kept to read, such as `"8MB"`. `"1MB"` by default, at most `"64MB"` |

Without a name, Luna gives the process one that starts with `~`. Starting a name that is already running fails.

## Come back to it by name

`attach` returns the process of that name, in any later call. It does not check that the process exists, so the first request to it fails if there is none.

```ts luna/lunas/user.ts
devOutput: h({}, async (ctx) => {
  const dev = ctx.sandboxes.box.attach("dev");
  const out = await dev.text({ wait: "5s" });
  return { stdout: out.stdout, lost: out.lost, exit: out.exit };
}),
```

## Read its output

`read` returns what the process wrote since the last read, as bytes, and `text` returns the same as strings. With `wait`, it waits up to that long, at most a minute, for something new or for the end.

| Field | Holds |
| --- | --- |
| `stdout`, `stderr` | What it wrote since the last read |
| `lost` | Bytes it wrote that were dropped before they were read |
| `exit` | How it ended, once everything it wrote has been read. `null` until then |

Each `SandboxProcess` remembers where its last read ended, in `at`. One made by `attach` starts from the oldest output still kept. Set `at` to `{ stdout: 0, stderr: 0 }` to read everything kept again.

`exit` holds `exitCode`, or `signal` when a signal ended the process.

## Keep up with its output

A process keeps the last `buffer` bytes of each output. When it writes faster than you read, the oldest bytes are dropped and `lost` counts them.

```ts luna/lunas/user.ts
const p = await ctx.sandboxes.box.spawn(["npm", "test"], { name: "test", buffer: "8MB", env: { CI: "1" } });
```

Read more often, or raise `buffer`, when `lost` is not 0.

## Write to its input

`write` sends text or bytes to the process's input, and `end` closes it. `resize(cols, rows)` sets the size of its terminal.

```ts luna/lunas/user.ts
const repl = ctx.sandboxes.box.attach("repl");
await repl.write(`${line}\n`);
const out = await repl.text({ wait: "1s" });
```

## Stop a process

`kill` sends a signal, `TERM` unless you name another, to the process and everything it started. `wait` returns how it ended, waiting up to a minute, or `null` while it still runs.

```ts luna/lunas/user.ts
const dev = ctx.sandboxes.box.attach("dev");
await dev.kill("INT");
const exit = await dev.wait("10s");
if (exit === null) await dev.kill("KILL");
```

`remove` ends the process if it runs, and forgets it and its output.

## List its processes

`processes()` returns those running and those that ended whose output is kept. Luna keeps an ended process for 10 minutes, and remembers at most 256 processes in a sandbox.

| Field | Holds |
| --- | --- |
| `name`, `pid`, `command` | Which process it is |
| `pty`, `keepAwake` | How it was started |
| `startedMs` | When it started, in milliseconds since the Unix epoch |
| `running`, `exit` | Whether it runs, and how it ended |
| `stdout`, `stderr` | How many bytes it has written to each |

## Keep the sandbox awake

A process alone does not keep a sandbox from going idle. When the sandbox stops, its processes end with it, unless it was declared with `persist: "freeze"`.

`keepAwake: true` keeps the sandbox running while the process runs, with nothing else using it. The luna stays awake with it, so a forgotten process keeps costing until it ends.

## Next steps

- [Connect to a sandbox](/files-and-code/connect-to-a-sandbox) joins an app's terminal to a process.
- [Run a sandbox](/files-and-code/run-a-sandbox) covers sizes, files and what a sandbox keeps.
- [sandbox](/sdk/sandbox) lists every method and type.
