> ## Documentation Index
> Fetch the complete documentation index at: https://axe-engine.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Start processes

> Start detached or attached processes, name and label them, retry starts safely, and set their environment, directory, user and retention.

The examples use the `axectl` wrapper from the [quickstart](https://axe-engine.mintlify.site/quickstart.md). The gRPC examples use `grpcurl` against `axe.v1` on port 9090; add `-H "authorization: Bearer $TOKEN"` when axed requires a token. The [native API reference](https://axe-engine.mintlify.site/reference/native-api.md) covers every field.

## Choose a lifetime

A process either outlives the call that started it or dies with it. Pick the RPC that matches.

| RPC | `axectl` | Lifetime | Record after exit |
| - | - | - | - |
| `Processes.Start` | `run` | Detached. Keeps running when every client disconnects. | Kept for reattach |
| `Processes.Run` | `exec` | Attached. Killed when the stream ends before the process exits, unless `detach` is set. | Kept for reattach |
| `Processes.Exec` | none | Attached, unary. Killed if the call is cancelled. Returns buffered output. | Not kept |

> [tab] axectl
>
> ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> axectl run --name build -- make -j8     # detached: prints the id and returns
> axectl exec -- ls -la                   # attached: stops if axectl goes away
> ```
>
> `axectl exec` always keeps stdin open and forwards yours, so you can pipe input into it. If `axectl exec` is killed, axed kills the whole process tree, and `ps` later shows the process with exit code 137.

> [tab] gRPC
>
> ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> grpcurl -plaintext -d '{"spec": {"command": ["make", "-j8"]}, "name": "build"}' \
>   localhost:9090 axe.v1.Processes/Start
>
> grpcurl -plaintext -d '{"start": {"spec": {"command": ["ls", "-la"]}}}' \
>   localhost:9090 axe.v1.Processes/Run
>
> grpcurl -plaintext -d '{"spec": {"command": ["sh", "-c", "echo out; exit 3"]}}' \
>   localhost:9090 axe.v1.Processes/Exec
> ```
>
> `Start` returns a `Process` once the process is running. `Run` streams `ProcessEvent`s: `started`, then `output` and `gap` events, then `exited`. Set `"detach": true` on `RunRequest` to keep the process running if the stream breaks. `Exec` returns `exit`, `stdout`, `stderr`, `*_truncated` and `elapsed`; `stdout_limit_bytes` and `stderr_limit_bytes` cap what it keeps of each stream, and 0 means the daemon limit of 256 MiB.

> [!NOTE]
>
> In the Agent Sandbox API, `process.v1.ProcessService/Start` behaves like `Run` without `detach`, so the process dies when the stream ends. `Execute` and `ExecuteWithLimits` behave like `Exec`. The Agent Sandbox API has no detached start. See the [Agent Sandbox API reference](https://axe-engine.mintlify.site/reference/agent-sandbox-api.md).

## Refer to a process

Every process gets an id of 32 hex digits that is never reused. Ids belong to one daemon instance; see [Snapshots](https://axe-engine.mintlify.site/snapshots.md) for what happens after a cold boot.

You can also give a process a name:

* A name is unique among **running** processes. Starting a second process with a name that is in use fails with `ALREADY_EXISTS`.
* The name frees when the process exits, so you can reuse it right away.
* A name resolves to the running process with that name, or, if none is running, to the most recently finished one that axed still keeps.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
axectl run --name build -- make
axectl run --name build -- make
# axectl: already exists (NAME_IN_USE): name "build" belongs to running process 44f52f99...
```

In the API, every call that takes a process uses `ProcessRef`, a oneof of `id` and `name`. `axectl` treats any argument of exactly 32 hex digits as an id and anything else as a name, so avoid names that look like ids.

## Label and filter

Labels are key-value pairs you attach at start. `List` returns only processes that carry **all** the labels you pass.

> [tab] axectl
>
> ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> axectl run -l job=ci -l team=web -- make test
> axectl ps -l job=ci
> ```

> [tab] gRPC
>
> ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> grpcurl -plaintext -d '{"spec": {"command": ["make", "test"]}, "labels": {"job": "ci", "team": "web"}}' \
>   localhost:9090 axe.v1.Processes/Start
> grpcurl -plaintext -d '{"labels": {"job": "ci"}}' localhost:9090 axe.v1.Processes/List
> ```

`List` includes finished processes that axed still keeps, so you can read their exit status.

## Retry a start safely

A connection can drop after axed started a process but before you got the reply, for example across a snapshot. Set `request_id` on `StartRequest` (or on the `start` inside `RunRequest`, or on `ExecRequest`) so that a retry does not start a second copy. Repeating a start with the same `request_id` returns the process the first start created, with no new process and no name conflict.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
grpcurl -plaintext -d '{"spec": {"command": ["make"]}, "name": "build", "request_id": "deploy-42"}' \
  localhost:9090 axe.v1.Processes/Start
```

The retry must be the same request: a different `spec`, `name`, `labels` or `retain_bytes` under the same id fails with `ALREADY_EXISTS` (`REQUEST_ID_CONFLICT`), so a reused key never silently returns an unrelated process.

Once axed evicts the record (see [finished records](https://axe-engine.mintlify.site/processes/output.md#finished-process-records)), a retry fails with `FAILED_PRECONDITION` (`REQUEST_EXPIRED`) instead of starting the command again. axed remembers the last 4096 evicted ids. After you remove a process with `Remove`, its id is free for a new start. `axectl run --request-id KEY` sets one.

### Retry an Exec

`Exec` with a `request_id` keeps its process like `Start` does: it shows in `List`, and it runs to the end even if the call is cancelled, as it is when a snapshot drops the connection. Repeating the same `Exec` returns the first run's exit and output, read back from its retained output (`retain_bytes`, default `--retain`), without running the command again. If the first run wrote more than it retained, or its record was evicted, the repeat fails with `FAILED_PRECONDITION` (`REQUEST_EXPIRED`). Size `retain_bytes` to the output you expect.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
grpcurl -plaintext -d '{"spec": {"command": ["make", "test"]}, "request_id": "test-42", "retain_bytes": 4194304}' \
  localhost:9090 axe.v1.Processes/Exec
```

## Set the environment, directory and user

These fields live in `Spec`, which every start RPC takes.

| Field | `axectl` | Behavior |
| - | - | - |
| `env` | `-e KEY=VALUE`, repeatable | Added to axed's own environment. |
| `cwd` | `-w DIR` | A relative path, or an absolute path under the root directory (`--root-dir`, default `/workspace`). Empty means the root. |
| `user` | `-u USER` | A name from `/etc/passwd`, a numeric uid, or `uid:gid`. Empty means `--default-user`, or axed's own user without it. |
| `terminal` | `-t` | Run on a pseudo-terminal. See [Terminals and input](https://axe-engine.mintlify.site/processes/terminals.md). |
| `stdin` | `-i` (on `run`) | Keep stdin open for later writes. Without it stdin is `/dev/null`. |
| `limits` | `--memory`, `--cpus`, `--timeout` | See [Signals, exits and limits](https://axe-engine.mintlify.site/processes/signals.md). |

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
axectl run -e MODE=release -w src -- make
```

axed resolves a relative working directory inside the root, so `-w src` runs in `/workspace/src`. An absolute path under the root, such as `-w /workspace/src`, is used as it is. An absolute path outside the root, such as `/` or `/etc`, fails with `PERMISSION_DENIED`, and so does `..` above the root. A directory that does not exist fails with `NOT_FOUND`. See [Path confinement](https://axe-engine.mintlify.site/files/confinement.md).

A uid with no `/etc/passwd` entry runs with a gid equal to the uid. An unknown user name fails with `NOT_FOUND`. Switching users needs privilege. axed sets the process's uid and gid, so when axed itself runs unprivileged (as in the default image, user 1000), a `user` that changes either fails with `PERMISSION_DENIED`. To run processes as another user, run axed as root or give it `CAP_SETUID` and `CAP_SETGID`.

> [!NOTE]
>
> The Agent Sandbox `ProcessConfig` carries `command`, `env_vars` and `cwd`. It has no user, name, labels or request id. axed takes an absolute `cwd` outside the root as relative to it, so `/src` there means `/workspace/src`.

## Set retention per process

Each process keeps its newest output bytes for replay. `retain_bytes` (`axectl run --retain N`) sets how many. [Retention](https://axe-engine.mintlify.site/processes/output.md#retention) describes the defaults, the limits and how axed raises small values.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
axectl run --retain 16MiB --name build -- make    # keep the last 16 MiB
```

Retention counts against a global output budget. [The output log](https://axe-engine.mintlify.site/processes/output.md) explains replay, the budget, and what happens when it fills.


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