Skip to main content
commands.run() returns everything at once when the command ends, which is all a quick command needs. Commands that run longer are worth bounding, watching, or feeding input to while they run. These options cover that; they layer onto the same run() call and none of them change its return value.

Timeouts

Both commands.run() and code.run() accept a timeout parameter, in seconds. When the timeout is reached, the guest work is interrupted and the call returns instead of waiting for it to finish.
A shell command that hits its timeout returns with exit_code 124:

Interrupting a command

A command no longer has to run to its timeout. interrupt() stops whatever is in the foreground, and the call that was waiting returns with exit code 130:
The command really stops. This is not the caller walking away while the guest keeps working, so the sandbox is yours again immediately. Ctrl-C works too. It used to do nothing while a command was running, because KeyboardInterrupt could not land on a thread blocked inside the emulator. It now reaches the command, which makes an interactive REPL behave the way you would expect.
code.run() cannot be interrupted yet, so use timeout there.There is no signal parameter, and none is needed: run() blocks, so Ctrl-C reaches it directly and timeout already covers deadlines. The TypeScript SDK needs an AbortSignal because its call returns a promise and the caller keeps going.A command that ignores the interrupt runs to its own deadline and is recovered the same way a timeout is.

Streaming output

commands.run() hands back everything at once, when the command ends. For a build or a test run that is a long wait followed by a wall of text, so it also takes on_stdout and on_stderr, called with each piece of output as it arrives:
The return value does not change. What the callbacks receive concatenates to exactly the stdout you get without them, so you can add one to existing code and nothing else moves. Chunks arrive as the guest produces output, not on a fixed schedule. A command printing two hundred lines with no pauses delivers them in one chunk; one printing a line a second delivers a chunk per line. A command that finishes quickly delivers a single chunk, which is indistinguishable from not streaming, and that is correct: there was nothing to stream. Streaming composes with interrupts. The chunks that arrived before the stop are kept, and the result still carries exit code 130.
code.run() does not stream, and does not accept on_stdout. Shell commands are what tend to run long enough to be worth watching.

Sending input

stdin gives the command something to read. A str or bytes becomes that command’s entire input, ending with a real EOF:
Input is byte-exact, so binary works the same way:
Without stdin a command’s input is closed, which is why cat with no stdin returns at once rather than hanging.

Streaming input

Pass a queue.Queue when what you send next depends on what came back. That puts the command on a terminal, so you write as it runs and putting None ends its input. run() blocks, so put from on_stdout or from another thread:
Match on the output so far rather than the chunk you were handed, and step through once: the prompt comes back after every answer. stdin accepts a str, bytes, a queue.Queue, or any iterable. An iterable is read one chunk at a time as the command runs, and running out ends the input:
Use tty=True for a terminal with no input of its own, and timeout=0 for no deadline, which is what an interactive session wants. Ctrl-C works throughout, and interrupts the command in the guest.
Close the stream only once the command has read what you sent it, by putting your input in response to output as above. Closing early is ignored and the command keeps waiting. If you have all the input up front, pass a string instead.