Foundational Compute

Run commands

Run shell commands in the foreground for the output, or in the background.

Run one command

Commands run in a shell, so pipes and redirects work.

Python · TypeScript · CLI

run.py
from fdtn import Sandbox

sbx = Sandbox.create("base")
result = sbx.commands.run("python3 -c 'print(1 + 1)'")
print(result.stdout)      # 2
print(result.exit_code)   # 0
sbx.kill()

The result has stdout, stderr, exit_code (exitCode) and error.

Command options

Python times are seconds. TypeScript times are milliseconds.

PythonTypeScriptCLI (fdtn exec)DefaultWhat it does
backgroundbackground-b, --backgroundoffReturn at once with a handle
envsenvs-e KEY=VALUE (repeat it)noneEnvironment variables
cwdcwd-c, --cwdHome folderWorking folder
useruser-u, --userThe sandbox userLinux user, for example root
on_stdout, on_stderronStdout, onStderralways onnoneCallback for each piece of output
stdinstdinpiped inputoffKeep standard input open
timeouttimeoutMsno limit60 s (SDK)Longest run time. 0 means no limit.
$ fdtn exec -c /app -e CI=1 -u root <id> npm test

The SDK stops a command after 60 seconds by default. Set timeout for long jobs.

To give every command the same variables, set envs at create: Sandbox.create("base", envs={"APP_ENV": "test"}) or fdtn create base -d -e APP_ENV=test. A per-command value wins.

If you pause and resume the sandbox, pass envs (or -e) with each command after the resume.

Stream output

Pass a callback to get output as it arrives.

Python · TypeScript

result = sbx.commands.run(
    "for i in 1 2 3; do echo step $i; sleep 1; done",
    on_stdout=lambda data: print("out:", data, end=""),
    on_stderr=lambda data: print("err:", data, end=""),
)

fdtn exec always streams. Pipe it like a local command.

$ echo hello | fdtn exec <id> cat   # reads your stdin

Output is UTF-8 text. For binary data, use fdtn cp or base64.

Run in the background

Use background mode for servers and long jobs. The handle has:

  • pid
  • wait(): returns the result; raises on a non-zero exit code
  • kill()
  • disconnect(): stop reading output; the command keeps running
  • send_stdin(data) / sendStdin(data): needs stdin on

Python · TypeScript · CLI

handle = sbx.commands.run("sleep 5; echo done", background=True, timeout=0)
print("pid", handle.pid)
# ... do other work ...
result = handle.wait(on_stdout=lambda d: print(d, end=""))
print(result.exit_code)

Set timeout 0 for a server, or the SDK stops it after 60 seconds. fdtn exec -b sets no limit.

For running commands, sbx.commands has list(), connect(pid), kill(pid) and send_stdin(pid, data). See Python and TypeScript.

Exit codes and errors

A non-zero exit code raises an error with the code and output.

Python · TypeScript

from fdtn import CommandExitException, TimeoutException

try:
    sbx.commands.run("ls /missing")
except CommandExitException as e:
    print(e.exit_code)   # 2
    print(e.stderr)      # ls: cannot access '/missing': ...
except TimeoutException:
    print("the command ran longer than its timeout")

To continue after a failure, add || true or catch the error. Other errors: Python, TypeScript.

fdtn exec exit codes

CodeMeaning
0 to 255The exit code of the command
128 + NSignal N ended it, for example 130 after Ctrl-C
1The command could not start (unknown sandbox ID)
141The reader closed the pipe (| head -1)

fdtn exec rules

  • Put options before the sandbox ID. Everything after the ID goes to the command.
  • Run a command that starts with a dash through sh -c.
  • Use @name for a sandbox named with --name.
  • No command time limit. The sandbox lifetime still applies.
$ fdtn exec @worker sh -c './-weird-program --flag'

For many sandboxes, use fdtn each. For async Python, use AsyncSandbox with the same options.

Next