Example: Background process
You use this pattern when a command outlasts a single synchronousexec 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 fromrun. 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 readrun_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 readexitCode / exit_code.
Step 3: (Optional) Attach and stream until exit
You callconnect 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
runwhen you want the platform to stop runaway processes automatically (semantics depend on server;0often means no limit: confirm in OpenAPI). - You await
wait(or handle errors) before youremovethe sandbox if you care about exit status. - You do not confuse
exec(one-shot) withcommands.run(background).
Related guides
Background commands
REST paths and concepts
Code execution
One-shot exec and streaming

