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.
The shell command to execute.
Maximum execution time in seconds. The command is killed if it exceeds this.
Additional environment variables for this command.
Working directory for the command.
Returns: Promise<ProcessResult>
ProcessResult
| Field | Type | Description |
|---|
exitCode | number | Exit code of the process (0 = success) |
stdout | string | Standard output |
stderr | string | Standard 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'
);