Skip to content
Commands

Execute Commands in the Sandbox

Execute terminal commands in the sandbox, just like in a real Linux terminal.

Basic Usage

javascript
import { Sandbox } from '@e2b/code-interpreter'

async function main() {
  const sandbox = await Sandbox.create()
  const result = await sandbox.commands.run('ls -l')
  console.log(result)
  await sandbox.kill()
}

main()

Streaming Output

The SDK allows you to stream output in real-time during command execution, receiving standard output and error output via callbacks.

javascript
import { Sandbox } from '@e2b/code-interpreter'

async function main() {
  const sandbox = await Sandbox.create()

  const result = await sandbox.commands.run('echo hello; sleep 1; echo world', {
    onStdout: (data) => {
      console.log(data)
    },
    onStderr: (data) => {
      console.log(data)
    },
  })
  console.log(result)

  await sandbox.kill()
}

main()

## Run Commands in the Background

Run commands in the background within the sandbox, allowing you to execute long-running tasks asynchronously without blocking the main program.

```javascript
import { Sandbox } from '@e2b/code-interpreter'

async function main() {
  const sandbox = await Sandbox.create()

  // Start the command in the background
  const command = await sandbox.commands.run('echo hello; sleep 10; echo world', {
    background: true,
    onStdout: (data) => {
      console.log(data)
    },
  })

  // Kill the command
  await command.kill()

  await sandbox.kill()
}

main()