# Running commands in background - Documentation

> For the complete documentation index, see [llms.txt](/llms.txt). Markdown is available with `Accept: text/markdown` and `.md` URL variants.

Source: /docs/guides/sandbox-commands-background

# Running commands in background

Commands can be run in the background by passing the `background` option to `commands.run()`. This returns immediately and the command continues running in the sandbox.
You can later kill it with `commands.kill()`.

JavaScript & TypeScript

Python

```
import { Sandbox } from 'novita-sandbox/code-interpreter'

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()
```

```
from novita_sandbox.code_interpreter 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()

sandbox.kill()
```

Last modified on June 4, 2026
