Guides

Run a long command

Start a build, a test suite or a data load that takes minutes or hours, check it, read its output and stop it.

briq_exec waits for a command for up to a minute. Anything longer (a build, a test suite, a data load, a migration) runs with async: true: briq starts it, answers at once with an exec id, and you check it until it ends. First, connect your agent.

briq_exec({ briq: "bq_…", cmd: ["sh", "-c", "cd /app && npm ci && npm test"], async: true })
→ ex_7kq2m9x1p4ab running since 10:02 UTC; stopped at 11:02 UTC if still running

briq_exec_result({ briq: "bq_…", exec: "ex_7kq2m9x1p4ab" })
→ ex_7kq2m9x1p4ab exited 0 after 3m
  stdout: …
  1. The command runs in the briq, in the background, under sh.
  2. briq_exec_result waits up to 30 seconds for it to end, then answers either way. Call it again while it runs.
  3. Its answer has the state, the exit code once it ends, and the last 64 KB of stdout and stderr.

Read all of the output

The whole output stays in the briq, in /tmp/.briq/exec/<exec id>/out and err:

briq_files_get({ briq: "bq_…", path: "/tmp/.briq/exec/ex_7kq2m9x1p4ab/out" })

Over 1 MB, read part of it with briq_exec: ["tail", "-n", "200", "/tmp/.briq/exec/ex_…/out"].

Follow the output as it comes

Pass offset to read from a byte of the stream instead of its tail. Each answer gives the next offset: follow it, and you read every byte once, in order.

briq_exec_result({ briq: "bq_…", exec: "ex_…", offset: 0 })
→ stdout from byte 0 to 1840 (1840 bytes so far; read on with offset 1840):
  {"type":"system","subtype":"init",…}

briq_exec_result({ briq: "bq_…", exec: "ex_…", offset: 1840 })
  • The wait ends as soon as there is output past the offset.
  • One read returns 32 KB by default (maxBytes, up to 256 KB), cut at a whole character.
  • stream: "stderr" reads stderr instead.
  • The end: the answer says the command ended and there is nothing left.

Write to it while it runs

Start it with stdinOpen: true, then send input with briq_exec_input. Its stdin stays open between writes until you send eof.

briq_exec({ briq: "bq_…", cmd: ["claude", "-p", "--input-format", "stream-json",
  "--output-format", "stream-json", "--verbose"], async: true, stdinOpen: true })

briq_exec_input({ briq: "bq_…", exec: "ex_…", seq: 1,
  data: "{\"type\":\"user\",\"message\":{\"role\":\"user\",\"content\":\"Run the tests\"}}\n" })
→ written as seq 1

briq_exec_input({ briq: "bq_…", exec: "ex_…", seq: 2, eof: true })
  • Writes land in order, 64 KB at most each. base64 sends bytes instead of text.
  • seq is the last write's number plus one. A write sent again with the same seq is not written twice, so a retry after an error is safe.
  • Without seq, the write gets the next number.

Set its environment

briq_exec({ briq: "bq_…", cmd: ["codex", "app-server"], async: true, stdinOpen: true,
  clearEnv: true, env: { HOME: "/home/agent", CODEX_HOME: "/home/agent/.codex" } })
  • env sets variables for this command only, over the briq's own.
  • clearEnv starts it from the briq's PATH, HOME and LANG only, then env. A variable of the briq, such as an API key, does not reach it.
  • Values stay off the command line, and out of status and the audit log (names only).
  • Both work without async too.

Stop it

briq_exec_stop({ briq: "bq_…", exec: "ex_7kq2m9x1p4ab" })
→ ex_7kq2m9x1p4ab stopped on request

It stops the command and every process it started.

Set a deadline

timeoutMs stops the command when it runs out: one hour by default, 24 hours at most.

briq_exec({ briq: "bq_…", cmd: ["make", "release"], async: true, timeoutMs: 14400000 })

Over REST

EXEC=$(curl -sf "https://api.briq.cloud/v1/briqs/$ID/exec" -H "Authorization: Bearer $BRIQ_API_KEY" \
  -H "Content-Type: application/json" -d '{ "cmd": ["make", "test"], "async": true }' | jq -r .exec.id)
until STATE=$(curl -sf "https://api.briq.cloud/v1/briqs/$ID/execs/$EXEC?waitMs=45000" \
  -H "Authorization: Bearer $BRIQ_API_KEY") && [ "$(jq -r .exec.state <<<"$STATE")" != running ]; do sleep 5; done
jq -r .exec.output.stdout <<<"$STATE"
MethodPath
POST/v1/briqs/:id/execWith async: true: 202 { exec }. Also stdinOpen, env, clearEnv
GET/v1/briqs/:id/execs/:execId?waitMs={ exec }; waitMs waits up to 45000 for it to end
GET/v1/briqs/:id/execs/:execId?offset=&stream=&maxBytes=exec.read: data, nextOffset, eof
POST/v1/briqs/:id/execs/:execId/input{ data } or { base64 }, eof, seq → { seq, duplicate }
GET/v1/briqs/:id/execsThe briq's execs, newest first
DELETE/v1/briqs/:id/execs/:execIdStops it

Over REST, offset reads up to 256 KB by default.

States

StateMeaning
runningStill going
exitedEnded by itself; exitCode says how
timed_outStopped at its deadline
stoppedStopped by briq_exec_stop
lostThe machine restarted without it, or the briq was destroyed

When it does not work

  • an async exec needs sh in the image (or setsid): the image has neither (distroless, scratch). Run the command without async (up to a minute), or use an image that has them.
  • 10 commands are already running: a briq runs ten at most. Wait for one, or stop one.
  • warning: timeoutMs is at most 60000 without async: the command ran for a minute at most. Run it again with async: true.
  • lost: the machine started again from scratch and the command did not survive (a crash, or a stop and start of a briq too large to resume from memory). Start the command again.
  • the briq is stopped, so the command cannot be reached: briq_start the briq, then stop the command. A check on a stopped briq answers with the last state seen.
  • seq 3 skips a write: send the number the error names, the last one taken plus one.
  • the command has not read the input before this one yet: it is busy or does not read its stdin. Send the same write again in a moment, with the same seq.
  • started without stdinOpen: start the command again with stdinOpen: true.
  • clearEnv needs env or stdinOpen needs mkfifo: the image lacks the tool. Most images have both (coreutils or busybox).

Good to know

  • A running command keeps its stack awake, and billed, until it ends or reaches its deadline, whatever the sleep setting or a key's maxSleep. See Sleep.
  • Lost an exec id? briq_status lists the briq's running commands.
  • Checking is use too, like any call on the stack.
  • Input at the start: stdin works as with briq_exec. For input while it runs, see Write to it while it runs.
  • Writing is use, like reading.
  • The output files stay in the briq until it is destroyed.

How can we help?

Message to the briq team