// Run with a working directoryconst result = await sandbox.exec.run('ls -la', { cwd: '/app' });// Run with environment variablesconst result = await sandbox.exec.run('echo $MY_VAR', { env: { MY_VAR: 'hello' },});// Run with a timeoutconst result = await sandbox.exec.run('sleep 30', { timeout: 5 });// Chain commandsconst result = await sandbox.exec.run( 'cd /app && npm install && npm run build');
Start a long-running command and stream its output via callbacks. The command runs in the background and you get a session handle to interact with it.
Copy
Ask AI
const session = await sandbox.exec.start("npm run dev", { cwd: "/app", onStdout: (data) => { console.log("stdout:", new TextDecoder().decode(data)); }, onStderr: (data) => { console.error("stderr:", new TextDecoder().decode(data)); }, onExit: (exitCode) => { console.log(`Process exited with code: ${exitCode}`); },});// Send input to the processsession.sendStdin("some input\n");// Kill the process laterawait session.kill();// Or wait for it to finishconst exitCode = await session.done;
const devServer = await sandbox.exec.start("npm run dev", { cwd: "/app", onStdout: (data) => { const text = new TextDecoder().decode(data); if (text.includes("ready")) { console.log("Dev server is ready!"); } },});// Do other work while the server runs...// Stop it when doneawait devServer.kill();