Starting process inside a sandbox

Here are the basic operations you can do with the process inside the sandbox:

Start process

import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create({ template: 'base' })

const npmInit = await sandbox.process.start({
  cmd: 'npm init -y', 
})
await npmInit.wait()

console.log(npmInit.output.stdout)

await sandbox.close()

Stop process

import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create({
  template: 'base',
})

const npmInit = await sandbox.process.start({
  cmd: 'npm init -y',
})
await npmInit.kill() 
// There will be no output because we immediately kill the `npm_init` process
console.log(npmInit.output.stdout)

await sandbox.close()

Stream stdout

Set either stdout handler for the whole sandbox level or per process.

import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create({
  template: 'base',
  onStdout: (output) => console.log('sandbox', output.line), 
})

const proc = await sandbox.process.start({
  cmd: 'echo "Hello World!"',
})
await proc.wait()
// output: sandbox Hello World!

const procWithCustomHandler = await sandbox.process.start({
  cmd: 'echo "Hello World!"',
  onStdout: (data) => console.log('process', data.line), 
})
await procWithCustomHandler.wait()
// output: process Hello World!

await sandbox.close()

Stream stderr

Set either stderr handler for the whole sandbox level or per process.

import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create({
  template: 'base',
})

// This command will fail and output to stderr because Golang isn't installed in the cloud playground
const golangVersion = await sandbox.process.start({
  cmd: 'go version',
  onStderr: (output) => console.log('sandbox', output.line), 
})
await golangVersion.wait()
// output: [sandbox] /bin/bash: line 1: go: command not found

const procWithCustomHandler = await sandbox.process.start({
  cmd: 'go version',
  onStderr: (data) => console.log('process', data.line), 
})
await procWithCustomHandler.wait()
// output: process /bin/bash: line 1: go: command not found

await sandbox.close()

On process exit

Set either on exit handler for the whole sandbox level or per process.

import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create({
  template: 'base',
  onExit: () => console.log('[sandbox]', 'process ended'), 
})

const proc = await sandbox.process.start({cmd: 'echo "Hello World!"'})
await proc.wait()
// output: [sandbox] process ended

const procWithCustomHandler = await sandbox.process.start({
  cmd: 'echo "Hello World!"',
  onExit: () => console.log('[process]', 'process ended'), 
})
await procWithCustomHandler.wait()
// output: [process] process ended

await sandbox.close()

Send stdin

import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create({ template: 'base' })

// This example will print back the string we send to the process using `sendStdin()`

const proc = await sandbox.process.start({
  cmd: 'while IFS= read -r line; do echo "$line"; sleep 1; done',
  onStdout: (output) => console.log(output),
})
await proc.sendStdin('AI Playground\n') 
await proc.kill()

await sandbox.close()