The capsule.commands interface gives you full control over process execution inside a capsule. You can run commands synchronously and wait for the result, launch long-running processes in the background, or stream output in real time over a WebSocket connection.
Foreground execution
capsule.commands.run() executes a shell command and blocks until it exits. It returns a CommandResult with the full stdout, stderr, exit code, and wall-clock duration.
Options
Background execution
Pass background=True to launch a process without waiting for it to finish. You get a CommandHandle immediately with the process PID and a tag.
Pass a tag string to label a background process with a human-readable name. You can use the same tag later to find it in capsule.commands.list().
Streaming execution
capsule.commands.stream() opens a WebSocket connection and yields events as the process runs. Use this when you want to display output in real time instead of waiting for the process to finish.
When you pass args, the command is executed directly. When you omit args, the command string is passed to /bin/sh -c.
Stream event types
Connecting to a running background process
capsule.commands.connect() attaches to an already-running background process by PID and yields its output events, exactly like stream().
Listing running processes
capsule.commands.list() returns all background processes currently running in the capsule as a list of ProcessInfo objects.
Each ProcessInfo has:
Killing a process
Send SIGKILL to a background process by PID:
Complete example