Skip to main content

Example: Background process

You use this pattern when a command outlasts a single synchronous exec HTTP request: a dev server, a queue worker, a migration script that prints progress for minutes. You start the process with commands.run, you list, wait, connect, or kill as your orchestration requires.

What you accomplish

You receive a PID from run. You poll or block with wait. You optionally attach to stdout/stderr with connect. You kill stuck jobs. You remove the sandbox when nothing else needs that disk.

Concepts you use

  • Positional arguments (TypeScript): run(command, env?, cwd?, timeout?): not an options object.
  • Python responses: Many command APIs return VoidRunResponse; you read run_result.data.pid (field names match generated models).
  • REST mapping: Endpoints live under /sandboxes/{id}/commands/: see Background commands.

Prerequisites

You completed Examples overview.

Step 1: Create sandbox and start a background job

Step 2: Wait until the process exits

You block until the PID finishes (or errors). You read exitCode / exit_code.

Step 3: (Optional) Attach and stream until exit

You call connect when you want stdout/stderr as the process runs. In both official SDKs, connect reads the SSE stream until it closes (you typically do not also wait on the same PID unless your SDK explicitly supports overlapping attach semantics).

Step 4: (Optional) Kill a stuck PID

Step 5: List running processes (debug)

Step 6: Clean up

Practices you should follow

  • You set a non-zero timeout on run when you want the platform to stop runaway processes automatically (semantics depend on server; 0 often means no limit: confirm in OpenAPI).
  • You await wait (or handle errors) before you remove the sandbox if you care about exit status.
  • You do not confuse exec (one-shot) with commands.run (background).

Background commands

REST paths and concepts

Code execution

One-shot exec and streaming