Skip to main content
The exec command runs a shell command inside a sandbox and captures its output. The CLI exits with the same exit code as the remote command.

Running a Command

oc exec sb-abc123 -- echo "Hello, World!"
Output:
Hello, World!

oc exec <id> -- <command...>

Executes a shell command and waits for it to complete. Everything after -- is joined into the command string.
FlagTypeDefaultDescription
--cwdstringWorking directory for the command
--timeoutint60Maximum execution time in seconds
--envstringEnvironment variable (KEY=VALUE, repeatable)
Default output: Prints stdout and stderr directly to your terminal, then exits with the remote exit code. JSON output (--json): Returns the full ProcessResult object.

ProcessResult

When using --json, the output includes:
FieldTypeDescription
exitCodenumberExit code of the process (0 = success)
stdoutstringStandard output
stderrstringStandard error

Examples

Run with a Working Directory

oc exec sb-abc123 --cwd /app -- ls -la

Run with Environment Variables

oc exec sb-abc123 --env MY_VAR=hello -- echo $MY_VAR

Run with a Timeout

oc exec sb-abc123 --timeout 5 -- sleep 30
# Killed after 5 seconds, non-zero exit code

Check for Errors

oc exec sb-abc123 -- python3 script.py
if [ $? -ne 0 ]; then
  echo "Command failed"
fi

Chain Commands

oc exec sb-abc123 -- bash -c 'cd /app && npm install && npm run build'

Capture Output as JSON

RESULT=$(oc exec sb-abc123 --json -- node -e 'console.log("hello")')
echo $RESULT | jq '.stdout'

Install Packages

oc exec sb-abc123 -- apt update
oc exec sb-abc123 -- apt install -y python3 nodejs