Skip to main content

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). Blocking POST .../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, 0 means no timeout; maximum 900 when a limit applies.

Responses you will see

  • Run: success, pid, command (CommandRunResponse).
  • List: success, processes — each item can include pid, command, startTime, running, exitCode (ProcessInfo).
  • Wait: success, exitCode, optional error (CommandWaitResponse).
  • Kill: success, message (CommandKillResponse).
  • Attach: text/event-stream with events stdout, stderr, exit (and error when applicable).

Typical flows

  1. Fire and reap: run → wait → read exitCode (batch workers).
  2. Fire and stream: run → connect / attach (SSE) until exit (logs).
  3. Inspect: list to debug PIDs and commands.
  4. Stop: kill for stuck jobs or cancellation.
wait and attach both relate to the same PID: wait returns JSON when the process exits; attach consumes an SSE stream. For one PID, use either streaming or wait in the same way your SDK expects—avoid overlapping both unless you know your stack allows it.

SDK notes

  • TypeScript: sandbox.commands.run(command, env?, cwd?, timeout?) — positional arguments, not an options object. run returns pid on the response object (top level). list() returns { processes }. wait, kill, connect take the numeric PID.
  • Python: Same positional order (command, env, cwd, timeout); use None for unused slots. Responses are often VoidRunResponse: use result.data.pid, result.data.exit_code, etc.

Examples

Replace sandbox-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 from run. Replace <pid> with that number in CLI and cURL.

Attach and stream output (SSE)

Stream stdout / 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 wait for very short jobs; attach needs the process to still be producing output when you connect.
  • Teardown — remove() the sandbox when finished; kill is for in-session control.

Where to read more

Next steps

Shell execution

All execution modes and REST paths in one place

Background process example

Hands-on walkthrough

TypeScript SDK

sandbox.commands reference

API Reference

OpenAPI: Execution tag