- Run and wait. You send a command and Fibre answers when it has finished, with its exit code and its output.
This is the easiest option, and it suits quick things such as a short script,
lsor agitcommand. - Start it in the background. Fibre starts the command and answers straight away with a command ID. The command then runs for as long as it needs. While it runs, you can watch its output as it is written, send it input, or stop it. You can even keep it running after the sandbox goes to sleep. This suits installs, test runs, builds, servers and agents.
All the examples on this page use the same sandbox, so start by creating it:
Run and wait
To run a command and wait for the result, send the command and read the answer:Response
Response
response
- A command is a list: the program first, then its arguments. In the SDKs you can also pass a single string.
It then runs through the shell (
sh -c), so pipes,&&and$(...)all work. Over HTTP, wrap the command yourself when you need the shell:["sh", "-c", "..."]. - A failing command is not an error. If the command exits with a non-zero code, the call still succeeds and
you read the code in
exit_code(exitCodein TypeScript). Errors are reserved for problems with the request itself, such as an unknown sandbox. - Over HTTP, an exec stops after 30 seconds (the command is stopped and the answer is
504 exec_timeout) and keeps at most 4 MB of each output stream. If the output was cut,stdout_truncatedorstderr_truncatedistrue. An exec runs in the sandbox’s default directory with the sandbox’s environment. For anything longer, or to choose a directory or add variables, use a background command. - In the SDKs,
sbx.run()has no time limit, because it starts a background command and waits for it for you. Pass atimeoutin seconds if you want it stopped after a while, andcwdandenvif you need them.
Stream a test run as it happens
Background commands are at their best with work that takes a while, because you can watch it as it happens instead of waiting in the dark. In this example the sandbox gets a small Python project, installspytest, and runs
the tests while you watch each result arrive.
1
Put the project in the sandbox
2
Install pytest
Installing a package can take longer than the 30 seconds an exec allows, so it runs as a background command and
you wait for it to finish. It takes about 10 seconds. (The package goes into a virtual environment, which the
default image’s Python requires.)Over HTTP, starting the command answers at once with its ID and
"status": "running". To wait for it, follow
its output until it ends. (The next sections explain this output stream in detail.)curl: wait for it
Response
Response
response
3
Run the tests and watch
curl: stream it
Output
Output
output
stream
The output stream
A background command’s output is read fromGET /v1/sandboxes/{id}/commands/{command_id}/logs. Rather than one
big JSON document, the answer is a series of small JSON objects, one per line (a format called NDJSON). This lets
Fibre send you each piece of output as soon as it exists. There are three kinds of line:
You can control the stream with these query parameters:
In the SDKs,
command.output() handles all of this for you. It follows the stream by default, and if the
connection drops, it reconnects and continues from where it left off.
Check on a command
You can ask about a background command at any time, for example to see whether it has finished and with which exit code:Response
Response
response
status is one of these:
The status also tells you when the command started and ended (
started_at, ended_at) and how much output it has
written (stdout_bytes, stderr_bytes). To see every command in the sandbox, oldest first, call
GET /v1/sandboxes/{id}/commands, or sbx.commands() in the SDKs.
Feed it input: drive a Python REPL
Some programs wait for input while they run: an interactive interpreter, a database shell, an agent that reads instructions. To talk to one, start it withstdin: true. You can then write to its standard input as often as
you like while it runs.
In this example, a Python interpreter runs each line as it arrives, just as if you were typing into it:
curl: write, then close
Output
Output
output
- What you write is passed on exactly as it is, including any binary data. The answer tells you how many bytes
were written, for example
{"written": 19}. - Closing standard input tells the program there is no more input (an end-of-file). Most programs then finish their work and exit, as the interpreter does here.
- The interpreter’s
>>>prompts go tostderr, sostdoutcontains only the results.
psql, a language server, or an agent
that takes its instructions on standard input.
Stop a command
To stop a background command, send it a signal, as you would withkill on your own machine. The signal goes
to the command and to every process it started, so nothing is left running behind it.
By default Fibre sends TERM, which politely asks the program to stop. If anything is still running 5 seconds
later, Fibre sends KILL, which cannot be ignored. If your program needs to clean up before it exits, send INT
instead (the same signal as pressing Ctrl-C), and give it the chance to finish:
curl: stop it
Response
Response
response
If you already know how long a command should be allowed to run, set
timeout_sec when you start it (timeoutSec
in spawn()). When the time is up, Fibre stops it in the same way, and its status becomes killed.
Keep a program running
Some programs are meant to run indefinitely: a dev server, a database, a bot. They should keep running after the call that started them, and they should still be running after the sandbox has slept and woken up again. To achieve that, start them withkeep: true:
Response
Response
response
- A kept command runs on its own, so its status is
detached. Its output is written to the file named inlog, inside the sandbox, which you can read with the files API.pidis its process ID. - It carries on across sleep and wake. On the
standardruntime, this is only true for programs started withkeep. On thevmruntime every process survives a sleep. Sleep and wake explains why. - You stop it with
kill, like any other command. - To reach it from the internet, create the sandbox with an
exposed_port. See public URLs.
curl: stop the server
Working directory and environment
Two options set the context a command runs in.cwd is the directory it starts in. env adds environment
variables for this one command, on top of the sandbox’s own variables:
curl: its output
Output
Output
output
env out of its logs and records, so the key reaches only that command and the programs it starts.
After the sandbox sleeps
Commands and sleep work together in a few ways that are worth understanding:- A running command keeps the sandbox awake. A sandbox never goes to sleep in the middle of a test run or an install.
- Commands belong to the sandbox’s current awake period. Once the sandbox sleeps (which only happens after its
commands have ended), the list of commands, their status and their output are cleared. Calls about them then
answer
409 not_running. Programs started withkeep, and their log files, are not affected. If you need a command’s output later, write it to a file. - Starting a command wakes a sleeping sandbox first, like any other call. You do not need to wake it yourself.
Limits
For reference, these are the limits that apply to commands:Errors
If a command call fails, the answer’scode tells you why:
Clean up
Related
Files
Put code and data into a sandbox, and take results out.
Sleep and wake
What keeps a sandbox awake, and what survives a sleep.
Serve an app
Make a kept server reachable at the sandbox’s HTTPS address.
Claude Code in a sandbox
Run a coding agent as a background command.