# Running commands in background (/docs/commands/background)

<!-- agent-signals: reading_time_min: 1 · est_tokens: 457 · updated: 2026-07-30 -->
Related: [Streaming command output](/docs/commands/streaming.md), [Upload data to volume](/docs/volumes/upload.md)

To run commands in background, pass the `background` option to the `commands.run()` method. This will return immediately and the command will continue to run in the sandbox.
You can then later kill the command using the `commands.kill()` method.

<CodeGroup>
  <CodeBlockTabs defaultValue="JavaScript & TypeScript" groupId="javascript-typescript+python">
    <CodeBlockTabsList>
      <CodeBlockTabsTrigger value="JavaScript & TypeScript">
        JavaScript & TypeScript
      </CodeBlockTabsTrigger>

      <CodeBlockTabsTrigger value="Python">
        Python
      </CodeBlockTabsTrigger>
    </CodeBlockTabsList>

    <CodeBlockTab value="JavaScript & TypeScript">
      ```js highlight={7}  
      import { Sandbox } from 'e2b'

      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()
      ```
    </CodeBlockTab>

    <CodeBlockTab value="Python">
      ```python highlight={6}  
      from e2b import Sandbox

      sandbox = Sandbox.create()

      # Start the command in the background
      command = sandbox.commands.run('echo hello; sleep 10; echo world', background=True)

      # Get stdout and stderr from the command running in the background.
      # You can run this code in a separate thread or use command.wait() to wait for the command to finish.
      for stdout, stderr, _ in command:
          if stdout:
              print(stdout)
          if stderr:
              print(stderr)

      # Kill the command
      command.kill()
      ```
    </CodeBlockTab>
  </CodeBlockTabs>
</CodeGroup>
