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

# Execute Commands in Workloads

> Run commands inside workload containers without opening an interactive session.

The `cpln workload exec` command runs the command you give it inside a container and returns the output. Unlike [`connect`](https://docs.controlplane.com/guides/cli/workload/connect), which only opens a shell, `exec` runs any command and is designed for non-interactive commands and scripts, similar to `docker exec` or `kubectl exec`.

## When to use this

> [card] Run scripts
>
> Execute maintenance scripts, health checks, or one-off commands

> [card] Automation
>
> Run commands from CI/CD pipelines or monitoring systems

> [card] Quick commands
>
> Check environment variables, list files, or test connectivity

> [card] Scripted debugging
>
> Run diagnostic commands and capture output

## Prerequisites

> [accordion] CLI installed
>
> Install the Control Plane CLI. See [Installation](https://docs.controlplane.com/cli-reference/installation.md).

> [accordion] Running workload
>
> You need a running [workload](https://docs.controlplane.com/reference/workload) in at least one location. See the [Create a Workload](https://docs.controlplane.com/guides/create-workload.md) guide.

> [accordion] Required permissions
>
> You need `exec` permission on the workload. See [Workload Permissions](https://docs.controlplane.com/reference/workload#permissions).

## Basic usage

```bash theme={null}
cpln workload exec <workload> -- <command> [args...]
```

The `--` separates the CLI options from the command to execute.

## Options

| Option | Description |
| - | - |
| `workload` | The workload to run the command in |
| `--location` | Target location (defaults to first available in GVC) |
| `--replica` | Target a specific replica (defaults to the first replica available within the specified location) |
| `--container` | Target a specific container (defaults to first container) |
| `-i, --stdin` | Pass stdin to the container |
| `-t, --tty` | Allocate a TTY |
| `-q, --quiet` | Only print output from the remote session |

## Run a simple command

```bash theme={null}
cpln workload exec my-app -- echo "Hello, World!"
```

Output:

```text theme={null}
Hello, World!
```

## Run commands with arguments

```bash theme={null}
cpln workload exec my-app -- ls -la
```

## Target specific locations or replicas

> [tab] Specific location
>
> ```bash theme={null}
> cpln workload exec my-app --location aws-eu-central-1 -- cat /etc/hostname
> ```

> [tab] Specific replica
>
> ```bash theme={null}
> cpln workload exec my-app --replica my-app-7f8d9c-abc12 -- cat /etc/hostname
> ```

> [tab] Specific container
>
> ```bash theme={null}
> cpln workload exec my-app --container sidecar -- ps aux
> ```

## Interactive mode

Combine `-i` (stdin) and `-t` (TTY) for interactive commands:

```bash theme={null}
cpln workload exec my-app -it -- /bin/bash
```

> [!TIP]
>
> For interactive sessions, consider using `cpln workload connect` which provides a simpler experience.

## Pipe input to commands

Use `-i` to pipe data into a container:

```bash theme={null}
# Pipe a SQL file into psql
cat schema.sql | cpln workload exec postgres-db -i -- psql -U postgres

# Pipe JSON data to a script
echo '{"key": "value"}' | cpln workload exec my-app -i -- python process.py
```

## Quiet mode

Suppress CLI output and show only command output:

```bash theme={null}
cpln workload exec my-app -q -- cat /app/version.txt
```

Useful for capturing output in scripts:

```bash theme={null}
VERSION=$(cpln workload exec my-app -q -- cat /app/version.txt)
echo "Deployed version: $VERSION"
```

## Common workflows

### Check environment variables

```bash theme={null}
cpln workload exec my-app -- env | grep -E "^(DATABASE|API)"
```

### View logs

```bash theme={null}
cpln workload exec my-app -- tail -100 /var/log/app.log
```

### Check disk usage

```bash theme={null}
cpln workload exec my-app -- df -h
```

### Test network connectivity

```bash theme={null}
cpln workload exec my-app -- curl -s http://api-service:8080/health
```

### Run database queries

```bash theme={null}
cpln workload exec postgres-db -- psql -U postgres -c "SELECT count(*) FROM users;"
```

## Troubleshooting

> [accordion] Command not found
>
> The command may not exist in the container. Check what's available:
>
> ```bash theme={null}
> cpln workload exec my-app -- which bash
> cpln workload exec my-app -- ls /bin /usr/bin
> ```

> [accordion] Permission denied
>
> The command may require elevated permissions, or you lack `exec` permission on the workload.

> [accordion] No replicas found
>
> The workload may not be running:
>
> ```bash theme={null}
> cpln workload get my-app
> cpln workload replica get my-app
> ```

> [accordion] Command arguments parsed incorrectly
>
> Ensure `--` separates CLI options from the command:
>
> ```bash theme={null}
> # Correct
> cpln workload exec my-app -- ls -la
>
> # Incorrect - CLI will try to parse -la
> cpln workload exec my-app ls -la
> ```

## Next steps

> [card] [Connect to Workloads](https://docs.controlplane.com/guides/cli/workload/connect.md)
>
> Interactive shell access

> [card] [Copy Files](https://docs.controlplane.com/guides/cli/cpln-cp.md)
>
> Transfer files to/from workloads

> [card] [Port Forwarding](https://docs.controlplane.com/guides/cli/cpln-port-forward.md)
>
> Access workload ports locally

> [card] [Command Reference](https://docs.controlplane.com/cli-reference/commands/workload.md#workload-exec)
>
> Full exec command reference
