Skip to main content
The Commands module lets you run shell commands inside a sandbox and capture their output. Access it via sandbox.commands.

Running a Command

const result = await sandbox.commands.run('echo "Hello, World!"');

console.log(result.stdout);   // Hello, World!
console.log(result.stderr);   // (empty)
console.log(result.exitCode); // 0

sandbox.commands.run(command, opts?)

Executes a shell command and waits for it to complete.
command
string
required
The shell command to execute.
opts
RunOpts
Returns: Promise<ProcessResult>

ProcessResult

FieldTypeDescription
exitCodenumberExit code of the process (0 = success)
stdoutstringStandard output
stderrstringStandard error

Examples

Run with a Custom Working Directory

const result = await sandbox.commands.run('ls -la', {
  cwd: '/app',
});

Run with Environment Variables

const result = await sandbox.commands.run('echo $MY_VAR', {
  env: { MY_VAR: 'hello' },
});
console.log(result.stdout); // hello

Run with a Timeout

const result = await sandbox.commands.run('sleep 30', {
  timeout: 5, // kill after 5 seconds
});
console.log(result.exitCode); // non-zero (killed)

Check for Errors

const result = await sandbox.commands.run('python3 script.py');

if (result.exitCode !== 0) {
  console.error(`Command failed: ${result.stderr}`);
}

Chain Commands

const result = await sandbox.commands.run(
  'cd /app && npm install && npm run build'
);