Run a command
Runs a command in a running sandbox. There are three modes. Pick by how long the command runs and whether you need its output live.
A command that exits with a code other than 0 is still a 200. Read exit_code.
Foreground. The response holds exit_code, stdout, and stderr. The
command is killed after timeout_seconds (default 60), and the response then
has timed_out: true. Each of stdout and stderr is cut at 4 MiB: the call
still returns 200, with stdout_truncated or stderr_truncated set and the
response headers X-Meadow-Stdout-Truncated: true or
X-Meadow-Stderr-Truncated: true. Write large output to a file and download it.
Stream. The response is server-sent events. The same 60 second default timeout applies.
Decode each stdout and stderr data value as JSON to get the text. A chunk
is not always one line. A captured stream of a command that printed three lines,
wrote one line to stderr and exited with code 2:
A stream that ends with neither exit nor error was cut. Treat the outcome as
unknown.
Background. The call returns at once. The command keeps running in the
sandbox whether or not you stay connected, so a dropped connection loses
nothing. Read it with GET /v1/sandboxes/{sandbox_id}/execs/{exec_id}, send it
input with the stdin call, and stop it with DELETE. A background command has
no default timeout: it runs until it exits, is killed, or the sandbox ends. Give
it a timeout_seconds, because a running background command also keeps the
sandbox from going idle. Send Idempotency-Key on every background start you
may retry. Without it each start runs the command again.
Rules.
- A string
commandruns throughsh -c. An array runs as given, with no shell. - To detect a timeout read
timed_out(or the statustimed_out), never the exit code. The code differs by vendor: 143 was seen on Daytona and 124 on Modal. - A command that exits by itself leaves the processes it started in the
background running, in every mode and on both vendors. They run until they
exit, the sandbox ends, or you
DELETEthat background command. A timeout ends the command’s whole process group. ADELETEalso ends what the command left, asDELETE /v1/sandboxes/{sandbox_id}/execs/{exec_id}describes. The command and its children get the environment variableMEADOW_EXEC_IDwith the exec id. - A command is finished when the command itself exits, also when a process it
left behind still holds its
stdoutorstderropen. Meadow waits up to about one second for that output and then stops recording. What such a process writes later is discarded: its writes succeed, and nothing is added tostdoutorstderr. Redirect the output of a process you leave running to a file. - On Modal, foreground and stream commands need
shin the image, also whencommandis an array. - Background commands keep their state in
/tmp/.meadow-execinside the sandbox. Leave that directory alone. If another user in the sandbox creates that path first, background starts return 400 and name the directory. - On Modal, a command that cannot be started, for example because
cwddoes not exist, returns exit code 128 with the reason on stderr. - On Modal, ending the whole process group at a timeout needs
mkdir,mkfifo, a writable/tmpand one ofsetsid,python3, orperlin the image. Without them a timeout ends only the first process. - With
userset, do not use the names_mr,_mu_name, or_mu_modeinenv. - Unknown fields return 400, and so does a known field set to
null.
Scope: sandboxes.
Authentication
A Meadow API key, sent as Authorization: Bearer mdw_.... The header
x-api-key: mdw_... is accepted in its place.
Path parameters
The sandbox id returned by POST /v1/sandboxes. It must belong to the key’s project.
Headers
Makes a retry safe. A repeat with the same key and the same body returns the
result of the first call instead of starting again. The same key with a
different body returns 409. On POST /v1/keys, POST /v1/projects and
POST /v1/images a value is scoped to the API key that sent it: another key
that sends the same value starts its own request. The sandbox and exec
routes scope it to the API key too. On the exec route the header only has an
effect with background: true. See Idempotency in the introduction for how
long a value is kept.
The key is 1 to 255 bytes of printable ASCII. A blank, longer, or non-ASCII key, or the header sent twice, returns 400. A UUID works well.
Request
The command to run. A string runs through sh -c. An array is run as given, with no shell, and its first entry must not be empty.
Extra environment variables for this command, added to the sandbox’s own. Keys must not be empty or contain =.
User name or uid to run as. Default is the image’s user. Letters, digits, _, .
and -, not starting with -. A user that does not exist in the sandbox, or an
image with no tool to switch users, returns 400. When the image does not run
as root, env may not reach the command.
The command is killed when this many seconds pass. At least 1, with no maximum. Default is 60 for the foreground and stream modes. A background command has no default: it runs until it exits, is killed, or the sandbox ends.
Return the output live as server-sent events. Cannot be combined with background.
Return at once and keep the command running. Cannot be combined with stream.
Background only. Keeps the command’s stdin open so input can be sent later with
POST /v1/sandboxes/{sandbox_id}/execs/{exec_id}/stdin. It cannot be turned
on after the command starts.
Response headers
Meadow’s id for this request, req_ and 24 hex characters. Every response
carries it, including errors, streams and unknown routes. Every error in the
Error schema repeats it as request_id. Quote it when you report a problem.
true when stdout was cut at 4 MiB. Absent otherwise. Foreground only.
true when stderr was cut at 4 MiB. Absent otherwise. Foreground only.
Response
Foreground: the result as JSON. Stream: the events described above.
True when the command was killed at timeout_seconds.
True when stdout passed 4 MiB and only the first 4 MiB is returned. Absent otherwise.
True when stderr passed 4 MiB and only the first 4 MiB is returned. Absent otherwise.