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

# Processes and streaming

> Stream output as it happens, run servers in the background, talk to stdin, open a terminal.

## Stream output

Pass callbacks to receive output while the command runs:

```python stream.py theme={null}
from anona import Sandbox

with Sandbox.create("base") as sbx:
    sbx.commands.run(
        "for i in 1 2 3; do echo tick $i; sleep 0.5; done; echo done >&2",
        on_stdout=lambda line: print("out:", line.strip()),
        on_stderr=lambda line: print("err:", line.strip()),
    )
```

```typescript stream.ts theme={null}
import { Sandbox } from "@anona/sdk";

const sbx = await Sandbox.create("base");
await sbx.commands.run("for i in 1 2 3; do echo tick $i; sleep 0.5; done; echo done >&2", {
  onStdout: (line) => console.log("out:", line.trim()),
  onStderr: (line) => console.log("err:", line.trim()),
});
await sbx.kill();
```

## Background processes

`background=True` returns a handle at once. The process keeps running after your script exits, for as long as the sandbox lives; reconnect to it by PID from anywhere.

```python background.py theme={null}
from anona import Sandbox

with Sandbox.create("base") as sbx:
    server = sbx.commands.run("sleep 300", background=True)
    print("pid", server.pid)

    for p in sbx.commands.list():
        print(p.pid, p.cmd, p.args)

    sbx.commands.kill(server.pid)
```

```typescript background.ts theme={null}
import { Sandbox } from "@anona/sdk";

const sbx = await Sandbox.create("base");
const proc = await sbx.commands.run("sleep 300", { background: true });
console.log("pid", proc.pid);
console.log((await sbx.commands.list()).map((p) => p.pid));
await sbx.commands.kill(proc.pid);
await sbx.kill();
```

```bash background.sh theme={null}
PID=$(anona-compute exec "$SANDBOX_ID" --background -- 'sleep 300')
echo "started $PID"
anona-compute exec "$SANDBOX_ID" -- "kill $PID"
```

## Write to stdin

```python stdin.py theme={null}
from anona import Sandbox

with Sandbox.create("base") as sbx:
    proc = sbx.commands.run("read name; echo hello $name", background=True, stdin=True)
    sbx.commands.send_stdin(proc.pid, "world\n")
    print(proc.wait().stdout)
```

## Terminals (PTY)

A PTY gives you a real terminal: colours, line editing and full-screen programs.

```python pty.py theme={null}
from anona import Sandbox
from e2b import PtySize

with Sandbox.create("base") as sbx:
    out = []
    term = sbx.pty.create(PtySize(rows=24, cols=80))
    sbx.pty.send_stdin(term.pid, b"echo $TERM; exit\n")
    term.wait(on_pty=out.append)
    print(b"".join(out).decode(errors="replace"))
```

From the terminal, `anona-compute connect` opens an interactive login shell in the sandbox over the same channel, with no SSH server involved:

```text theme={null}
anona-compute connect $SANDBOX_ID       # Ctrl-] then d detaches; the shell keeps running
```

To watch a terminal in a browser instead, see [Live view](https://docs.anonalabs.com/compute/guides/live-view.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.