Skip to content

Run commands ​

Use sandbox.commands.run in JavaScript/Python or sandbox.Commands.Run in Go to start a command and wait for it to finish. The result contains standard output, standard error, and the exit code.

mjs
import { Sandbox } from "@abox-dev/sdk";

const sandbox = await Sandbox.create();

try {
  const result = await sandbox.commands.run("printf 'hello from AgentBox\\n'");
  console.log(result.stdout);
} finally {
  await sandbox.kill();
}

JavaScript and Python run the command through a shell. Pass user input through envs instead of inserting it into the command string. Go runs the executable with CommandOptions.Args. To use shell syntax, invoke a shell explicitly.

You can also set the working directory, environment variables, user, and timeout. A non-zero exit code raises CommandExitError; it includes the command output and exit code. See the SDK reference for all options.

Select a user ​

Set user in JavaScript/Python to run a command as a specific sandbox user. In Go, set CommandOptions.User for Run or Start, or PTYOptions.User when creating a terminal. This also works with streaming output.

mjs
import { Sandbox } from "@abox-dev/sdk";

const sandbox = await Sandbox.create();

try {
  const result = await sandbox.commands.run("whoami", { user: "root" });
  console.log(result.stdout);
} finally {
  await sandbox.kill();
}

An omitted user uses the template's configured user. Go treats an empty User as omission. Connecting to an existing process keeps the user selected when it was started.

See Go command options for the full API.