> ## 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.

# Signals, exits and limits

> Send signals to process groups, wait for exits and read exit codes, set running-time timeouts, and cap memory, CPU, tasks and open files.

## Process groups and trees

Every process axed starts leads its own process group. A process on a terminal also leads its own session. Children it starts join that group unless they create their own, as a shell with job control or `setsid` does.

When axed manages a delegated cgroup v2 tree, each process also gets its own cgroup, which holds every descendant no matter what group or session it joins. `axectl info` shows the cgroup, or `-` without one. [Containers and hosts](https://axe-engine.mintlify.site/deploy/containers.md) explains how to give axed a cgroup.

## Send a signal

`Processes.Signal` delivers any positive signal number to the process group. Set `leader_only` to signal only the process you started; axed then uses a pidfd, so the signal cannot reach a recycled pid. Once the process you started has exited, a signal succeeds and does nothing, even while axed is still collecting its output, so it never reaches a process that has since reused the pid or its group id.

> [tab] axectl
>
> ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> axectl kill build              # SIGTERM to the process group
> axectl kill -s KILL build      # or -s 9
> axectl kill -s HUP build
> ```
>
> `-s` takes a number or one of `HUP`, `INT`, `QUIT`, `KILL`, `USR1`, `USR2`, `TERM`, `STOP`, `CONT` and `WINCH`. Names are case-insensitive, and the `SIG` prefix is optional: `TERM`, `term`, `SIGTERM` and `sigterm` all work. `axectl kill` always signals the group.

> [tab] gRPC
>
> ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> grpcurl -plaintext -d '{"process": {"name": "build"}, "signal": 15}' \
>   localhost:9090 axe.v1.Processes/Signal
> grpcurl -plaintext -d '{"process": {"name": "build"}, "signal": 1, "leader_only": true}' \
>   localhost:9090 axe.v1.Processes/Signal
> ```
>
> A signal of 0 or less fails with `INVALID_ARGUMENT`.

> [!NOTE]
>
> The Agent Sandbox `SendSignal` accepts only `SIGINT`, `SIGTERM` and `SIGKILL`, and always signals the group.

## Kill a whole tree

A signal reaches only the process group. Descendants that left the group, or that ignore the signal, survive it. axed kills the whole tree with `SIGKILL` when:

* a timeout expires,
* you remove a running process with `Processes.Remove` and `kill: true` (`axectl rm -f`),
* the stream of an attached `Run` (without `detach`) or an `Exec` call ends before the process exits.

With a cgroup, the kill uses `cgroup.kill` and reaches every descendant. Without one, axed kills the process group and every descendant it can find through `/proc`. If the process you started has already exited, axed kills only what is left in its process group.

When the process you started exits on its own, anything it left running in its group or cgroup keeps running while axed keeps its record. With a cgroup, axed kills those leftovers when it forgets the record. Without one, they keep running after axed forgets the record, because once the leader has exited axed cannot safely tell its group from an unrelated one. If leftovers matter, run with cgroups or stop the process before it exits. axed does not keep processes started with `Exec` without a `request_id` or through the Agent Sandbox API, so it kills their leftovers when they exit. Without a cgroup, it kills what is left in the group the moment the leader is reaped, while the group can still be told apart, so output the leftovers write afterwards is lost. With a cgroup, it kills them once their output has drained.

## Wait for an exit

`Processes.Wait` returns the `Process` once it has exited and its output is complete. `axectl wait` prints why the process ended and exits with its exit code.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
axectl kill build
axectl wait build; echo $?     # signaled 143, then 143
```

The `Exit` message says how the process ended:

| Field | Meaning |
| - | - |
| `exit_code` | The exit status, or 128 plus the signal number when a signal killed it. SIGTERM gives 143 and SIGKILL 137. |
| `signal` | Set only when a signal killed the process. |
| `reason` | Why it ended, below. |

| `reason` | `axectl ps` | Meaning |
| - | - | - |
| `EXIT_REASON_EXITED` | `exited` | The process exited on its own. |
| `EXIT_REASON_SIGNALED` | `signaled` | A signal ended it, including one you sent with `Signal` (`axectl kill`). |
| `EXIT_REASON_TIMED_OUT` | `timed-out` | axed killed the tree because its timeout expired. |
| `EXIT_REASON_OOM_KILLED` | `oom-killed` | The kernel killed the tree for exceeding its memory limit. Only reported with cgroups. |
| `EXIT_REASON_KILLED` | `killed` | You removed it while running, with `Remove` and `kill` (`axectl rm -f`). |
| `EXIT_REASON_OWNER_GONE` | `owner-gone` | The `Run` stream (without `detach`) or `Exec` call that owned it ended before it exited. |
| `EXIT_REASON_SHUTDOWN` | `shutdown` | axed stopped it while shutting down. |
| `EXIT_REASON_UNKNOWN` | `unknown` | The leader was reaped but its exit status was lost. `exit_code` is `-1`. |

When axed kills a tree, the first cause it acted on wins: a process that times out and is then removed reports `TIMED_OUT`. If the process had already exited on its own when axed acted, it keeps `EXITED` or `SIGNALED`.

## Read resource usage

`Process.usage` reports CPU time and memory. For a running process, `Get` and `List` read it when they describe the process, so it is current; axed never samples in the background. Once the process exits, it holds the totals.

| Field | Running | Exited |
| - | - | - |
| `user_cpu`, `system_cpu` | CPU time so far | Total CPU time |
| `memory_bytes` | Memory in use now | Unset |
| `peak_memory_bytes` | The tree's peak, with a cgroup | The peak |
| `tasks` | Tasks (threads) now | Unset |
| `scope` | `USAGE_SCOPE_TREE` or `USAGE_SCOPE_LEADER` | The same |

With a cgroup the figures cover the whole tree (`cpu.stat`, `memory.current`, `memory.peak`, `pids.current`). Without one they cover only the process you started, from `/proc`, and `scope` says `USAGE_SCOPE_LEADER`: children it forked are not counted. `axectl ps` shows CPU seconds and memory in its `CPU` and `MEM` columns, with a `*` after leader-only figures.

## Set a timeout

`Limits.timeout` (`axectl --timeout`) kills the tree after the process has **run** for that long. Time the sandbox spends suspended does not count, so a sandbox restored after an hour in a snapshot does not wake up and kill its jobs. [Snapshots](https://axe-engine.mintlify.site/snapshots.md) explains how axed measures running time.

> [tab] axectl
>
> ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> axectl run --name t --timeout 2s -- sleep 30
> axectl wait t; echo $?     # 137
> axectl ps                  # STATE timed-out
> ```
>
> `--timeout` takes durations such as `500ms`, `30s` or `1m30s`.

> [tab] gRPC
>
> ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> grpcurl -plaintext -d '{"spec": {"command": ["sleep", "30"], "limits": {"timeout": "2s"}}}' \
>   localhost:9090 axe.v1.Processes/Start
> ```
>
> A zero timeout fails with `INVALID_ARGUMENT`. Leave the field unset for no timeout.

> [!NOTE]
>
> The Agent Sandbox equivalent is `time_limit` on `ExecuteWithLimits`, bounded by axed's `--execute-default-time-limit` and `--execute-max-time-limit`. It uses the same running-time clock.

## Limit resources

`Spec.limits` takes four resource limits. Each must be positive; 0 fails with `INVALID_ARGUMENT`. How axed enforces them depends on whether it has a cgroup.

| Field | `axectl` | With a cgroup | Without a cgroup |
| - | - | - | - |
| `memory_bytes` | `--memory SIZE` (bytes, or `KiB`, `MiB`, `GiB`) | `memory.max` for the whole tree, with swap disabled. Exceeding it OOM-kills the tree, reported as `EXIT_REASON_OOM_KILLED`. | `RLIMIT_AS` on each process. Allocations past it fail inside the program, and axed reports nothing special. |
| `cpu_millis` | `--cpus CPUS` (a CPU count such as `1.5`, sent as 1500) | `cpu.max`; 1000 is one CPU. | Refused with `FAILED_PRECONDITION`. |
| `max_tasks` | none | `pids.max` for the whole tree. | `RLIMIT_NPROC`, which the kernel counts per user, not per tree. |
| `open_files` | none | `RLIMIT_NOFILE` on each process, soft and hard. Without it a process gets the soft limit axed started with. | Same. |

The operator can set daemon-wide defaults for requests that leave a limit unset, and ceilings no request may exceed: `--default-timeout`, `--default-memory`, `--default-max-tasks`, `--max-timeout` and `--max-memory`. A request over a ceiling fails with `INVALID_ARGUMENT` (`LIMIT_EXCEEDED`). See [Default policies](https://axe-engine.mintlify.site/reference/axed.md#default-policies).

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
axectl run --memory 512MiB --cpus 1.5 -- make -j4
```

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
grpcurl -plaintext -d '{"spec": {"command": ["make"], "limits": {"memory_bytes": 536870912, "max_tasks": 256, "open_files": 1024}}}' \
  localhost:9090 axe.v1.Processes/Start
```

A memory-hungry command shows the difference:

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
axectl exec --memory 30000000 -- dd if=/dev/zero of=/dev/null bs=100M count=1
# with a cgroup: killed, exit 137, STATE oom-killed
# without:       dd: memory exhausted by input buffer of size 104857600 bytes (100 MiB), exit 1
```

> [!WARNING]
>
> A cgroup needs kernel support for each controller. On a guest kernel built without the `cpu` controller, axed refuses CPU limits even though it has a cgroup. See [Platform requirements](https://axe-engine.mintlify.site/reference/platform-requirements.md).

The Agent Sandbox API has no resource limits; the platform sets them for the whole sandbox.

## Shutdown

When axed shuts down (SIGINT, or SIGTERM with the default `--on-sigterm=shutdown`), it stops accepting starts, sends SIGTERM to every running process group, waits up to two seconds (or `--shutdown-timeout` if shorter), and then kills what is left.


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