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

# Execs

> Run commands in a running sandbox, wait for their exit codes, send signals, and close their stdin. Execs keep running when clients disconnect.

An exec is a process started in a running sandbox. It keeps running when the client that started it disconnects, and its output can be replayed later from any machine. Execs live as long as their sandbox.

## Start an Exec

```bash CLI theme={null}
telnyx-edge sandbox-gpu exec "$SANDBOX_ID" --workdir /app --env BATCH_SIZE=64 --timeout 3600 \
  -- python eval.py --checkpoint /app/ckpt
```

```bash curl theme={null}
curl -X POST "https://api.telnyx.com/v2/ai/sandboxes/$SANDBOX_ID/execs" \
  -H "Authorization: Bearer $TELNYX_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $(uuidgen)" \
  -d '{
    "command": ["python", "eval.py", "--checkpoint", "/app/ckpt"],
    "workdir": "/app",
    "env": {"BATCH_SIZE": "64"},
    "timeout": 3600
  }'
```

* **CLI:** `exec` streams the command's stdout and stderr to yours and exits with its exit code. With `-i`, your stdin is passed to the command; with `--detach` (`-d`), the command starts and its exec ID is printed.

Both send an `Idempotency-Key`. The CLI also resumes reading output after a dropped connection; with curl, read the output through the [exec attach WebSocket](https://developers.telnyx.com/docs/gpu-sandboxes/attach.md#exec-attach).

| Field | Required | Description |
| - | - | - |
| `command` | Yes | Argv. At most 4,096 arguments, each at most 131,071 bytes in UTF-8, 1 MiB in total, and no NUL byte (`422 invalid_command` otherwise). |
| `workdir` | No | Absolute working directory. Defaults to the sandbox's. |
| `env` | No | Extra variables on top of the sandbox's environment, with the same rules as on create (`422 invalid_env`). |
| `timeout` | No | Stop the process after this many seconds (status `terminated`, reason `timeout`). Default: no limit. An exec never outlives its sandbox. |

Through the REST API, the call returns `201` at once, without waiting for the command or streaming its output. The exec is `running`, or `exited` with its `exit_code` if it already finished.

* If the sandbox can't confirm the start within a few seconds, the exec comes back `starting` and the start completes in the background. A start that can't be confirmed within 5 minutes ends `failed` with reason `agent_unavailable`.
* The sandbox must be `running` (`409 sandbox_not_running` otherwise).
* Send an `Idempotency-Key`. A retry with the same key returns the same exec instead of running the command twice, and completes a start that is still `starting`.

## Statuses

```text theme={null}
starting ──► running ──► exited
    │            └─────► terminated
    └──► failed
```

| Status | Meaning |
| - | - |
| `starting` | The start isn't confirmed yet. |
| `running` | The process is running. |
| `exited` | The process ended. `exit_code` is set: 128 plus the signal number when a signal killed it. |
| `terminated` | The process was stopped because its sandbox ended (`sandbox_terminated`) or its `timeout` was reached (`timeout`). |
| `failed` | The process was never confirmed started. See `reason`. |

| `reason` | Meaning |
| - | - |
| `sandbox_terminated` | The sandbox ended, or was ending, first. |
| `timeout` | The exec's `timeout` was reached. |
| `agent_unavailable` | The sandbox didn't confirm the start in time. |
| `cancelled` | `SIGKILL` was sent before the start was confirmed. |
| `agent_error` | The sandbox returned an inconsistent answer. |

An exec that is `failed` with `agent_unavailable` or `cancelled` can still move on to `running`, `exited`, or `terminated` if its start did reach the sandbox.

## Wait for an Exec

`GET /v2/ai/sandboxes/{sandbox_id}/execs/{exec_id}?wait=10` is a long poll: it returns `200` as soon as the exec has ended, or after at most 10 seconds. Call it again until `status` is `exited`, `failed`, or `terminated`. Without `wait`, it returns at once. If the sandbox can't answer, the response carries the last known state.

`GET /v2/ai/sandboxes/{sandbox_id}/execs` lists a sandbox's execs, newest first, optionally filtered by repeated `status` parameters. A status in the list can lag the process by up to about 15 seconds; `GET …/execs/{exec_id}` asks the sandbox.

## Output and Stdin

Exec output isn't part of the sandbox's [logs](https://developers.telnyx.com/docs/gpu-sandboxes/logs.md). Read it, and write to the exec's stdin, through the [exec attach WebSocket](https://developers.telnyx.com/docs/gpu-sandboxes/attach.md#exec-attach). The CLI's `exec` and `execs attach` commands use it for you. Each exec keeps at least its last 64 MiB of output for replay, within a per-sandbox total of 1/16 of the sandbox's memory, at most 256 MiB. That retained output counts against the sandbox's memory.

`POST /v2/ai/sandboxes/{sandbox_id}/execs/{exec_id}/stdin/close` sends EOF to the exec's stdin, once the input already accepted has been written. Disconnecting from the attach WebSocket never sends EOF: a process reading stdin keeps waiting until it gets `stdin_eof` or this call. Closing twice, or closing an exec that ended, changes nothing. The CLI's equivalent is `telnyx-edge sandbox-gpu execs close-stdin`.

## Send a Signal

```bash CLI theme={null}
telnyx-edge sandbox-gpu execs kill "$SANDBOX_ID" "$EXEC_ID" --signal SIGTERM
```

```bash curl theme={null}
curl -X POST "https://api.telnyx.com/v2/ai/sandboxes/$SANDBOX_ID/execs/$EXEC_ID/kill" \
  -H "Authorization: Bearer $TELNYX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"signal": "SIGTERM"}'
```

The signal goes to the exec's process group. `signal` is one of `SIGTERM`, `SIGKILL` (default), `SIGINT`, `SIGHUP`, `SIGQUIT`, `SIGUSR1`, or `SIGUSR2`.

* The call is asynchronous: it returns `202` with the exec as it is right after. With `SIGKILL`, the exec ends shortly after with `status: exited` and `exit_code: 137`.
* Signalling an exec that already ended changes nothing.
* An exec still `starting` can only be cancelled: `SIGKILL` ensures the command never runs, and it ends `failed` with reason `cancelled`. Any other signal fails with `409 exec_not_started`; retry once the exec is `running`.
* With an `Idempotency-Key`, a retry replays the first answer. Two overlapping requests with the same key may both deliver the signal.

## Activity

Starting, running, and finishing execs, and writing to an exec's stdin, count as activity for the sandbox's `idle_timeout`. See [Timeouts](https://developers.telnyx.com/docs/gpu-sandboxes/lifecycle.md#timeouts).
