Skip to navigation

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.

ModeRequestResponseUse for
Foregrounddefault200 JSON when the command exitsShort commands.
Streamstream: true200 text/event-streamWatching output as it is written.
Backgroundbackground: true202 with an exec idAnything long, such as an agent run.

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.

EventdataWhen
stdoutA JSON string with a chunk of outputZero or more times.
stderrA JSON string with a chunk of outputZero or more times.
exit{"exit_code": 2}, plus "timed_out": true when the command was killed at its timeoutOnce, last.
errorAn Error objectOnce, in place of exit, if the stream breaks.

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:

event: stdout
data: "line 1\n"
event: stdout
data: "line 2\n"
event: stdout
data: "line 3\n"
event: stderr
data: "warn\n"
event: exit
data: {"exit_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 command runs through sh -c. An array runs as given, with no shell.
  • To detect a timeout read timed_out (or the status timed_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 DELETE that background command. A timeout ends the command’s whole process group. A DELETE also ends what the command left, as DELETE /v1/sandboxes/{sandbox_id}/execs/{exec_id} describes. The command and its children get the environment variable MEADOW_EXEC_ID with the exec id.
  • A command is finished when the command itself exits, also when a process it left behind still holds its stdout or stderr open. 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 to stdout or stderr. Redirect the output of a process you leave running to a file.
  • On Modal, foreground and stream commands need sh in the image, also when command is an array.
  • Background commands keep their state in /tmp/.meadow-exec inside 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 cwd does 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 /tmp and one of setsid, python3, or perl in the image. Without them a timeout ends only the first process.
  • With user set, do not use the names _mr, _mu_name, or _mu_mode in env.
  • Unknown fields return 400, and so does a known field set to null.

Scope: sandboxes.

curl -sS -X POST "$MEADOW_BASE_URL/v1/sandboxes/$SANDBOX_ID/exec" \
-H "Authorization: Bearer $MEADOW_API_KEY" \
-H "Content-Type: application/json" \
-d '{"command":["python3","-c","print(6*7)"],"timeout_seconds":30}'

Authentication

AuthorizationBearer

A Meadow API key, sent as Authorization: Bearer mdw_.... The header x-api-key: mdw_... is accepted in its place.

Path parameters

sandbox_idstringRequired

The sandbox id returned by POST /v1/sandboxes. It must belong to the key’s project.

Headers

Idempotency-KeystringOptionalformat: "^[ -~]+$"1-255 characters

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

At most 1 MiB.
commandstring or list of stringsRequired

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.

cwdstringOptional
Working directory, at most 4096 bytes. Default is the image's working directory.
envmap from strings to stringsOptional

Extra environment variables for this command, added to the sandbox’s own. Keys must not be empty or contain =.

userstringOptional<=64 characters

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.

timeout_secondsintegerOptional

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.

streambooleanOptionalDefaults to false

Return the output live as server-sent events. Cannot be combined with background.

backgroundbooleanOptionalDefaults to false

Return at once and keep the command running. Cannot be combined with stream.

stdinbooleanOptionalDefaults to false

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

x-request-idstringOptional

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.

X-Meadow-Stdout-TruncatedenumOptional

true when stdout was cut at 4 MiB. Absent otherwise. Foreground only.

Allowed values:
X-Meadow-Stderr-TruncatedenumOptional

true when stderr was cut at 4 MiB. Absent otherwise. Foreground only.

Allowed values:

Response

Foreground: the result as JSON. Stream: the events described above.

exit_codeinteger
The exit code. Not reliable after a timeout.
stdoutstring
Everything the command wrote to stdout, up to 4 MiB.
stderrstring
Everything the command wrote to stderr, up to 4 MiB.
timed_outbooleanOptional

True when the command was killed at timeout_seconds.

stdout_truncatedbooleanOptional

True when stdout passed 4 MiB and only the first 4 MiB is returned. Absent otherwise.

stderr_truncatedbooleanOptional

True when stderr passed 4 MiB and only the first 4 MiB is returned. Absent otherwise.

Errors

400
Bad Request Error
401
Unauthorized Error
403
Forbidden Error
404
Not Found Error
409
Conflict Error
413
Content Too Large Error
429
Too Many Requests Error
500
Internal Server Error
502
Bad Gateway Error