> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fermion.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Execute Long-Running Commands Inside Your Sandbox

> Execute long-running commands with real-time stdout and stderr streaming inside a Fermion Sandbox using the Python SDK.

The `run_streaming_command()` method is designed for executing **long-running or interactive commands** inside your sandbox while receiving **real-time output streams**.

Unlike `run_command()`, this method does not block until completion — it allows you to process live logs or incremental output as the command runs.

***

### `async run_streaming_command(cmd: str, args: Optional[List[str]] = None, stdin: Optional[str] = None, on_stdout: Optional[Callable[[str], None]] = None, on_stderr: Optional[Callable[[str], None]] = None) -> CommandResult`

**Description:**\
Executes a long-running command inside the sandbox and streams output in real time. Callbacks are triggered as new stdout or stderr data chunks arrive.

***

**Parameters:**

| Parameter   | Type                    | Required | Description                                        |
| ----------- | ----------------------- | -------- | -------------------------------------------------- |
| `cmd`       | `str`                   | Yes      | Command to execute (e.g., `"npm"`, `"python3"`)    |
| `args`      | `List[str]`             | No       | List of command arguments (defaults to empty list) |
| `stdin`     | `str`                   | No       | Optional standard input to send to the command     |
| `on_stdout` | `Callable[[str], None]` | No       | Callback function invoked when stdout data arrives |
| `on_stderr` | `Callable[[str], None]` | No       | Callback function invoked when stderr data arrives |

***

**Returns:**

`CommandResult` object with the following attributes:

* `stdout: str` - Complete accumulated output
* `stderr: str` - Complete accumulated error output
* `exit_code: int` - Process exit code (0 = success)

**Example**

```python theme={null}
import asyncio
from fermion_sandbox import Sandbox

async def main():
    sandbox = Sandbox(api_key="your-api-key")
    await sandbox.create(should_backup_filesystem=False)

    def handle_stdout(data: str):
        print("[stdout]", data.strip())

    def handle_stderr(data: str):
        print("[stderr]", data.strip())

    result = await sandbox.run_streaming_command(
        cmd="npm",
        args=["install", "express"],
        on_stdout=handle_stdout,
        on_stderr=handle_stderr
    )

    print("Exit code:", result.exit_code)

    # With stdin
    result = await sandbox.run_streaming_command(
        cmd="python3",
        args=["-c", 'import sys; [print(line.strip()) for line in sys.stdin]'],
        stdin="Hello from stdin\n"
    )

    print("Output:", result.stdout)

asyncio.run(main())
```
