Background commands
Use the Commands API when a workload should outlive a single HTTP request: dev servers, queue workers, install scripts that print for minutes, or anything you need to observe or stop later by process id (PID). BlockingPOST .../exec waits until the command finishes (or hits a timeout). exec-stream streams output but still ties the client to that request. Background commands return immediately with a PID so you can list, attach (SSE), wait, or kill from separate calls.
For a full comparison with sync
exec, streaming, PTY, and runCode, see Shell execution. Detached processes with a PID are started only via POST .../commands/run (and sandbox.commands.run in SDKs); use list / wait / kill / attach from the Commands API.When to use what
REST endpoints
All routes are under/sandboxes/{id}/commands/ (prefix with your API base, e.g. https://api.void-run.com/api). Authenticate with X-API-Key.
Request/response schemas are in the API Reference tab (e.g.
runBackgroundCommand, attachToBackgroundProcess, waitForBackgroundProcess).
Run request (/commands/run)
command(string, required): shell command line as executed in the sandbox.env(object, optional): extra environment variables.cwd(string, optional): working directory.timeout(integer, optional): seconds. Per OpenAPI,0means no timeout; maximum 900 when a limit applies.
Responses you will see
- Run:
success,pid,command(CommandRunResponse). - List:
success,processes— each item can includepid,command,startTime,running,exitCode(ProcessInfo). - Wait:
success,exitCode, optionalerror(CommandWaitResponse). - Kill:
success,message(CommandKillResponse). - Attach:
text/event-streamwith eventsstdout,stderr,exit(anderrorwhen applicable).
Typical flows
- Fire and reap:
run→wait→ readexitCode(batch workers). - Fire and stream:
run→connect/ attach (SSE) untilexit(logs). - Inspect:
listto debug PIDs and commands. - Stop:
killfor stuck jobs or cancellation.
SDK notes
- TypeScript:
sandbox.commands.run(command, env?, cwd?, timeout?)— positional arguments, not an options object.runreturnspidon the response object (top level).list()returns{ processes }.wait,kill,connecttake the numeric PID. - Python: Same positional order
(command, env, cwd, timeout); useNonefor unused slots. Responses are oftenVoidRunResponse: useresult.data.pid,result.data.exit_code, etc.
Examples
Replacesandbox-id in SDK strings and <sandbox-id> in CLI/cURL with your sandbox id. Set VR_API_KEY. cURL uses the hosted API host; on self-hosted, swap the URL for your VR_API_URL.
Start a background process (commands/run)
List running processes
Wait until a process exits
Use the PID returned fromrun. Replace <pid> with that number in CLI and cURL.
Attach and stream output (SSE)
Streamstdout / stderr until an exit event. If the process exits very quickly, attach may see little output—wait is safer for short jobs.
Kill a process
Full lifecycle (run → list → wait → kill)
Same pattern as Code execution § Background processes: start a process, inspect the list, either wait for exit or kill (not both required).Practices
- Sandbox must be running — start or resume the VM before
commands/run. - PID is scoped to the sandbox — do not reuse PIDs across sandboxes.
- Fast commands — prefer
waitfor very short jobs;attachneeds the process to still be producing output when you connect. - Teardown —
remove()the sandbox when finished;killis for in-session control.
Where to read more
- Shell execution — Commands API beside
exec, streaming, and PTY. - Example: Background process — step-by-step run → wait → connect → kill → list → cleanup.
- Code execution —
exec, interpreter, and streaming.
Next steps
Shell execution
All execution modes and REST paths in one place
Background process example
Hands-on walkthrough
TypeScript SDK
sandbox.commands referenceAPI Reference
OpenAPI: Execution tag

