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

# Command

> An asynchronous operation on a workload or volume set, such as running a cron workload now or expanding a volume. Create one, then follow its lifecycle stage until it completes, fails, or is cancelled.

Most of Control Plane is declarative: you describe a workload or volume set, and Control Plane keeps the running system matching the description. Some operations don't fit that model because they are one-off actions, like running a cron workload right now, stopping one replica, or expanding a volume. A command is the resource for those actions. You create it, Control Plane carries it out in the background, and the command records how far it got.

## How It Fits

A command belongs to the resource it acts on and is created at that resource's `-command` endpoint. The command's `type` names the action, and its `spec` holds the action's inputs.

| Resource | Endpoint | Command types |
| - | - | - |
| [Workload](https://docs.controlplane.com/concepts/workload.md) | `/org/ORG/gvc/GVC/workload/WORKLOAD/-command` | [`runCronWorkload`](https://docs.controlplane.com/reference/workload/types#run-now), [`stopReplica`](https://docs.controlplane.com/reference/workload/general#stop-a-replica) |
| [Volume set](https://docs.controlplane.com/reference/volumeset.md) | `/org/ORG/gvc/GVC/volumeset/VOLUME_SET/-command` | The volume, snapshot, and restore commands in [volume set commands](https://docs.controlplane.com/reference/volumeset.md#commands) |

Creating a command returns `201 Created` with a `Location` header that links to the new command. The work has started, but it hasn't finished: poll the command to see where it is. Creating a workload command requires the matching `exec` [permission](https://docs.controlplane.com/reference/workload/security.md#permissions), such as `exec.runCronWorkload`.

## Lifecycle

The `lifecycleStage` field shows where a command is. It starts at `pending` and normally ends in one of the three final stages.

| Stage | Meaning |
| - | - |
| `pending` | Accepted, not yet started. |
| `running` | Control Plane is carrying it out. |
| `cancellation-requested` | The command is being stopped, because Control Plane decided to (for example, a cron run passed its `activeDeadlineSeconds`) or a caller [cancelled it](#working-with-commands). Control Plane is removing what the command created. It normally ends in `cancelled`, but can still end in `completed` or `failed` if the work finished first. |
| `cancelled` | Final. Stopped, and everything it created has been removed. |
| `completed` | Final. Finished successfully. |
| `failed` | Final. It didn't finish. The command's `status` holds type-specific details. |

`completed` means the action itself succeeded, not just the request. For example, a `runCronWorkload` command completes only when the job it started has finished. `completed` and `cancelled` are permanent. In rare cases, a `failed` command is corrected to `completed` once Control Plane sees the work actually finished.

## Working with Commands

The examples use [`cpln rest`](https://docs.controlplane.com/cli-reference/commands/rest), which sends an authenticated request to any API path. Any HTTP client works the same way against `https://api.cpln.io`.

**Create** a command by posting its `type` and `spec`:

```bash theme={null}
cat > run-now.yaml <<EOF
type: runCronWorkload
spec:
  location: aws-us-west-2
EOF

cpln rest post /org/my-org/gvc/my-gvc/workload/my-cron-workload/-command --file run-now.yaml
```

**List** a resource's commands, or **get** one by its ID. The list is paginated, and `?lifecycleStage=STAGE` narrows it to one stage:

```bash theme={null}
cpln rest get /org/my-org/gvc/my-gvc/workload/my-cron-workload/-command
cpln rest get "/org/my-org/gvc/my-gvc/workload/my-cron-workload/-command?lifecycleStage=running"
cpln rest get /org/my-org/gvc/my-gvc/workload/my-cron-workload/-command/COMMAND_ID
```

Control Plane moves a command between stages. The one change a caller can make is to **cancel** a command by patching its `lifecycleStage` to `cancellation-requested`. Every volume set command, including a [volume copy](https://docs.controlplane.com/reference/volumeset.md#cancel-a-volume-copy), and `runCronWorkload` can be cancelled; `stopReplica` can't. This needs the same `exec.TYPE` permission as creating the command:

```bash theme={null}
echo '{"lifecycleStage": "cancellation-requested"}' > cancel.json
cpln rest patch /org/my-org/gvc/my-gvc/volumeset/my-volume-set/-command/COMMAND_ID --file cancel.json
```

A cancellation request is refused with `400 Command type 'TYPE' does not support cancellation`, or with `400 Cannot request cancellation for command in terminal state 'STAGE'` once the command has finished.

## Gotchas

* **A `201` doesn't mean the action finished.** It means the command was accepted. Read `lifecycleStage` until it reaches `completed`, `failed`, or `cancelled`.
* **A command is read-only once created, except for cancellation.** Any other `PATCH` from a user or service account is rejected with `403 Cannot modify commands`, and a `DELETE` isn't allowed. A command stays as a record of what ran until the workload or volume set it belongs to is deleted, which deletes its commands too.
* **Some commands conflict, and they conflict in different ways.**
  * A second `stopReplica` for the same replica is rejected with `400` while the first is still in progress.
  * A volume set command that overlaps one already in progress on the same volume is rejected with `409`. The exception is `deleteVolume`, which fails the overlapping commands instead.
  * `runCronWorkload` has no such check, so a second on-demand run starts alongside the first.

## Learn More

> [card] [Workload types: Run Now](https://docs.controlplane.com/reference/workload/types.md#run-now)
>
> The `runCronWorkload` spec, including per-run container overrides and deadlines.

> [card] [Stop a Replica](https://docs.controlplane.com/reference/workload/general.md#stop-a-replica)
>
> The `stopReplica` spec and what Control Plane does when a replica is stopped.

> [card] [Volume set commands](https://docs.controlplane.com/reference/volumeset.md#commands)
>
> Expand, shrink, copy, delete, snapshot, and restore the volumes of a volume set.

> [card] [Workload security](https://docs.controlplane.com/reference/workload/security.md#permissions)
>
> The `exec` permissions that allow a principal to create workload commands.
