Skip to main content
Running commands is most of what you do with a sandbox: installing packages, running tests, starting a server, launching an agent. Fibre gives you two ways to do it, and choosing the right one makes your code simpler.
  • 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, ls or a git command.
  • 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.
Here is how the two compare: 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
A few things are worth knowing:
  • 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 (exitCode in 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_truncated or stderr_truncated is true. 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 a timeout in seconds if you want it stopped after a while, and cwd and env if 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, installs pytest, 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
3

Run the tests and watch

curl: stream it
output
Here is the stream exactly as it arrived, one JSON object per line. The first piece came at once. The second one came two seconds later, when the slow test finished:
stream
Output reaches you in the pieces the program writes, at the moment it writes them. You see the test run as it happens, not only once it is over.

The output stream

A background command’s output is read from GET /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
A command’s 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 with stdin: 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
  • 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 to stderr, so stdout contains only the results.
The same approach works for any program that reads its input: a shell, 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 with kill 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
These are the options for stopping a command: 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 with keep: true:
response
  • A kept command runs on its own, so its status is detached. Its output is written to the file named in log, inside the sandbox, which you can read with the files API. pid is its process ID.
  • It carries on across sleep and wake. On the standard runtime, this is only true for programs started with keep. On the vm runtime 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
This is the right place for secrets that one command needs, such as a model API key for an agent. Fibre keeps the values of 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 with keep, 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’s code tells you why:

Clean up

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.