Streaming Output

Updated at:

The E2B SDK can process stdout and stderr while a command is running. This is useful for live logs, progress updates, and observing long-running tasks.

Listen to command output

const result = await sandbox.commands.run("for i in 1 2 3; do echo step-$i; sleep 1; done", {
  onStdout: (data) => {
    console.log("stdout:", data);
  },
  onStderr: (data) => {
    console.error("stderr:", data);
  },
});

console.log(result.exitCode);

Python example:

result = sandbox.commands.run(
    "for i in 1 2 3; do echo step-$i; sleep 1; done",
    on_stdout=lambda data: print("stdout:", data),
    on_stderr=lambda data: print("stderr:", data),
)

print(result.exit_code)

Recommendations

Streaming output works well for real-time frontend display or server-side log collection, but your business logic should still use the final exitCode to determine whether the command succeeded. Callback parameters are output chunks, so do not assume each callback maps to a complete log line. When log volume is high, truncate, sample, or persist logs on the application side instead of keeping the full output in memory.