Standard Input

Updated at:

Standard input lets you send data to a running command. It is useful for interactive programs, script parameter input, and long-running process control.

Start a command that accepts stdin

const handle = await sandbox.commands.run("cat", {
  background: true,
  stdin: true,
});

await sandbox.commands.sendStdin(handle.pid, "hello from stdin\n");
await sandbox.commands.closeStdin(handle.pid);

const result = await handle.wait();
console.log(result.stdout);

Python example:

handle = sandbox.commands.run(
    "cat",
    background=True,
    stdin=True,
)

sandbox.commands.send_stdin(handle.pid, "hello from stdin\n")
sandbox.commands.close_stdin(handle.pid)

result = handle.wait()
print(result.stdout)

closeStdin() closes stdin for a non-PTY command and sends EOF to the process. PTY sessions do not use closeStdin(). If you need to stop input in a PTY session, send Ctrl+D (0x04) or follow the interactive program's exit convention.

Recommendations

  • For simple parameters, prefer command arguments or environment variables.

  • For processes that require continuous input, persist the pid. When the task ends normally, close stdin and wait for the result first. Use kill() only on timeout or failure.

  • Do not write sensitive credentials directly into interactive flows where command output might expose them.