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

# Run commands with Exec

> Run bounded commands from the Console or SDK, interpret process results, and recover safely when execution is uncertain.

Exec runs one bounded command inside a running Computer or Sandbox. Use it for short diagnostics, builds that fit the timeout, scripts and verifiable file operations. Use [SSH](https://docs.deepest.dev/connect/ssh.md) for an interactive Computer shell or a user-managed long-running session.

The Console **Command** area is a sequence of independent executions, not a persistent terminal. Changing directory in one request does not establish a working directory for the next request.

## Run from the Console

Select the intended Machine and open **Command**. Confirm the Machine is running and account eligibility is active.

Enter a short command, such as:

```sh theme={null}
uname -s
```

Expect `Linux` in the output. The Console deliberately executes entered shell text through a shell. If you need several related shell actions, combine them in the same submitted command:

```sh theme={null}
cd "$HOME" && pwd && python3 --version
```

The command list is a view of responses in the mounted workspace. It is not a durable command-history or job-recovery service. After an uncertain request, a successful Machine query alone cannot prove whether the command's side effect occurred.

## Run through the SDK

Install the [SDK from npm](https://docs.deepest.dev/api-and-sdk/typescript.md) and provide the configured control URL, trusted caller key and intended Computer ID. This complete example prints a deterministic value without modifying the Guest:

```js theme={null}
import { Deepest } from "@deepest/sdk";

const { DEEPEST_API_URL, DEEPEST_API_KEY, DEEPEST_COMPUTER_ID } = process.env;
if (!DEEPEST_API_URL || !DEEPEST_API_KEY || !DEEPEST_COMPUTER_ID) {
  throw new Error("Set the configured control URL, secure key and Computer ID.");
}
const deepest = new Deepest({
  baseUrl: DEEPEST_API_URL,
  apiKey: DEEPEST_API_KEY,
});
const computer = deepest.computer(DEEPEST_COMPUTER_ID);
const state = await computer.get();
if (!state.ok || state.result.state !== "running") {
  throw new Error("Observe or explicitly start the original Computer first.");
}

const reply = await computer.exec({
  command: "printf",
  args: ["%s\n", "Deepest ready"],
  timeout_ms: 5000,
});
if (!reply.ok) {
  console.error({
    code: reply.error.code, retry: reply.retry,
    resource_id: reply.resource_id,
    retry_after_seconds: reply.error.retry_after_seconds,
  });
  process.exitCode = 1;
} else {
  const result = reply.result;
  console.log(result.stdout);
  if (result.exit_code !== 0 || result.timed_out || result.output_truncated) {
    console.error({
      exit_code: result.exit_code, timed_out: result.timed_out,
      output_truncated: result.output_truncated,
    });
    process.exitCode = 1;
  }
}
```

Expect `Deepest ready`, exit code zero, `timed_out: false` and `output_truncated: false`. The SDK returns the complete envelope; it does not turn nonzero process exits into automatic retries.

For a Sandbox, select the Sandbox by ID and use `deepest.sandbox(id).exec(...)` with the same request shape. Check the Sandbox's expiry and idle policy first.

## Executable and arguments are separate

The public API does not implicitly interpret shell operators. This request executes a program with arguments:

```js theme={null}
// Continue with the computer defined above.
const pythonReply = await computer.exec({
  command: "python3",
  args: ["-c", "print(2 + 3)"],
  timeout_ms: 5000,
});
if (!pythonReply.ok) throw new Error(`Exec failed: ${pythonReply.error.code}`);
if (pythonReply.result.exit_code !== 0 || pythonReply.result.timed_out ||
    pythonReply.result.output_truncated || pythonReply.result.stdout.trim() !== "5") {
  throw new Error("The expected complete Python result was not observed.");
}
console.log(pythonReply.result.stdout);
```

It should print `5`. A string such as `"cd /work && npm test"` is not an executable name.

If shell syntax is actually needed, request a shell explicitly:

```js theme={null}
// Continue with computer. Replace the path with an existing authorized directory.
const shellReply = await computer.exec({
  command: "/bin/sh",
  args: ["-lc", "cd /path/to/project && git status --short"],
  timeout_ms: 5000,
});
if (!shellReply.ok) throw new Error(`Exec failed: ${shellReply.error.code}`);
if (shellReply.result.exit_code !== 0 || shellReply.result.timed_out ||
    shellReply.result.output_truncated) {
  throw new Error("Inspect the shell result before running another command.");
}
console.log(shellReply.result.stdout);
```

Validate untrusted inputs before placing them in a shell program. Prefer separate arguments when possible. No API field sets a persistent working directory for later calls; place the directory change in the same shell invocation.

The current request timeout range is 1–900,000 milliseconds. The executable and argument sizes are bounded by the Reference. For Sandbox Exec, the actual budget is additionally capped by its hard expiry and any account entitlement expiry.

## Interpret two layers of result

| Layer | Meaning | What to inspect |
| - | - | - |
| API envelope | Whether a valid operation result is known | `ok`, `resource_id`, `retry`, or `error.code` |
| Process result | What the command actually did | `stdout`, `stderr`, `exit_code`, `timed_out`, `output_truncated` |

A completed command with exit code 1 is still an observed command result. For example, a test failure is not a network failure. A truncated output may hide diagnostics; do not report a complete transcript. A known timeout does not prove that the command made no changes before timing out.

Exec success is `no-retry` because the command may have effects. Even a seemingly harmless command can invoke network services or write files.

## Concurrency and retries

Computer Exec, Sandbox Exec, Tools installation/inspection and Desktop setup share the account's command capacity. Opening more Machines does not multiply this capacity.

`EXEC_BUSY` uses HTTP 429 and `retry: "safe"`: the request was refused before command dispatch. Wait at least `error.retry_after_seconds` (the same value as HTTP `Retry-After`) and explicitly retry the still-needed command. Do not invent a global “requests per minute” quota from this concurrency response.

For `UNKNOWN / query-first`, preserve the command, target ID and relevant task context. Query the original Machine and then inspect the specific intended effect. If a request was supposed to create a file, read that file; if it submitted a third-party action, use that system's read-only status check. A Machine being running does not resolve the command outcome.

There is no public durable Exec ID or command-status endpoint. Do not poll an invented `jobs.get()` or replay a mutation until its effect is known.

## Long-running work

For a Computer job that needs to outlive a single HTTP request, manage the process inside Linux through SSH and an appropriate installed session/process manager. Deepest does not automatically turn Exec into a durable job.

Here is a small, user-managed background job with separate output and a completion record. Run this **inside the Computer through SSH**. It creates a fresh directory rather than overwriting a project. Confirm that the standard `nohup` utility and Python are available:

```bash theme={null}
command -v nohup
python3 --version

job_dir="$(mktemp -d "$HOME/deepest-job.XXXXXX")"
cat > "$job_dir/task.py" <<'PY'
import json
import time

time.sleep(20)  # Leave time to disconnect and reconnect for this example.
print(json.dumps({"sum": sum(range(1, 100001))}))
PY

(
  cd "$job_dir" || exit 1
  nohup sh -c 'python3 task.py > output.json 2> stderr.log
code=$?
printf "%s\n" "$code" > exit-code.txt
exit "$code"' </dev/null > launcher.log 2>&1 &
  printf '%s\n' "$!" > pid.txt
)
printf 'Job directory: %s\n' "$job_dir"
```

Save the printed directory path. You can exit SSH and reconnect to the same Computer; do not Stop it during the job. After the example has had time to finish, inspect the saved directory:

```bash theme={null}
job_dir='<SAVED_JOB_DIRECTORY>'

if test -f "$job_dir/exit-code.txt"; then
  status="$(cat "$job_dir/exit-code.txt")"
  printf 'Exit code: %s\n' "$status"
  cat "$job_dir/stderr.log"
  if test "$status" != 0; then
    printf 'The task failed. Inspect its effects before another submission.\n'
  else
    python3 - "$job_dir/output.json" <<'PY'
import json
import sys

with open(sys.argv[1], encoding="utf-8") as source:
    result = json.load(source)
assert result["sum"] == 5000050000, result
print("Verified sum:", result["sum"])
PY
  fi
else
  printf 'No completion record yet. Inspect the process and logs before retrying.\n'
fi
```

Expect exit code `0`, an empty `stderr.log`, and `Verified sum: 5000050000`. The launcher PID is diagnostic information, not a durable platform job ID or proof of success. If the completion file is absent or the exit code is nonzero, inspect the logs and the specific intended effect before another submission. Do not assume that a disappeared process never ran.

For your own build or script, replace the example task with the authorized workload and use that workload's output checks. Keep credentials out of command arguments and logs. This pattern does not restart work after Stop or preserve RAM execution state; stronger supervision is a separate Linux configuration you own.

Ordinary control-service reconnects may interrupt access without stopping the Guest workload. Stop, however, ends Guest processes. Starting again is a cold boot; only services configured to start on boot restart under their own Linux configuration.

A foreground Sandbox Exec prevents idle cleanup during that execution and refreshes activity, but it never extends the fixed 24-hour lifetime. A detached background process alone does not refresh Sandbox idle activity.

## Troubleshoot and finish

| Symptom | Action |
| - | - |
| `NOT_RUNNING` | Observe the original Machine, then explicitly Start only if appropriate |
| `INVALID_REQUEST` | Check command/args/timeout against the generated Reference |
| `EXEC_BUSY` | Wait for capacity and the indicated delay; retry only this request |
| Nonzero exit code | Read the program's output and fix the actual command or project |
| Timeout or truncated output | Inspect effects/logs; choose a shorter step or SSH workflow |
| `UNKNOWN` | Query the original task effect before another mutation |
| Tool install reports failure | Use [Tools inspection](https://docs.deepest.dev/environment/agent-tools.md); installation and login are separate |

When finished, retain required files and explicitly Stop an otherwise unused Machine. Stopping frees running Memory but not Storage. For exact schemas and error codes, use [API Reference](https://docs.deepest.dev/api-reference.md), [errors](https://docs.deepest.dev/api-and-sdk/errors.md) and [reliability](https://docs.deepest.dev/api-and-sdk/reliability.md).


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