# terminals

Canonical URL: https://developers.iagenify.com/sdk/ref/connected-storage/terminals/
Language: en

`storage.terminals → LocalTerminals`

Shells and servers on a computer that outlive a single answer. A dev server started here is still running on the next call.

Status: a computer only · lives in this process · piped, no PTY

A command run and awaited dies with its answer. A dev server needs the opposite: start it, leave it, come back to its output. That is what a terminal is here.

```javascript
const pc = await api.storage.get({ id: machineId });

const shell = await pc.terminals.create({ name: "dev", cwd: "myapp" });

await shell.run("npm run dev");
await shell.output({ since: 0 });   // what it has printed so far

shell.busy;   // whether a command is still running
shell.cwd;    // where it is now — it follows cd

await shell.interrupt();            // Ctrl+C
await shell.kill();
```

## The surface {#surface}

| Call | Does |
| --- | --- |
| terminals.list() | Which ones are open. |
| terminals.get(id) | One of them. |
| terminals.create({ name, cwd, command }) | Opens a shell, optionally running something at once. |
| terminals.remove(id) | Closes one. |
| terminal.run(line) | Sends a command. |
| terminal.output({ since }) | What it has printed, from an offset. |
| terminal.interrupt() | Ctrl+C — stops the command, keeps the shell. |
| terminal.kill() | Ends the shell. |

## They live in the process that runs the SDK {#process}

> **Restart your program and every terminal is gone**
>
> They are children of your process, not of the machine. A dev server started through one dies when the process holding it exits — including on a reload in watch mode.

Two consequences worth designing around: a second instance of your app has its own terminals and cannot see the first's, and a terminal is not a way to keep something running after you are done.

## Never write to a shell that is busy {#busy}

A shell runs one command at a time. Sending a line while one is running pushes text into whatever is reading stdin — which is a prompt, an editor, or nothing at all.

```javascript
if (shell.busy) {
  await shell.interrupt();     // or wait, but do not just send
}
await shell.run("npm test");
```

## Reading output as it comes {#output}

`output({ since })` returns what was printed from an offset, so polling is a loop with a moving cursor rather than a re-read of everything.

```javascript
let seen = 0;

for (let i = 0; i < 60 && shell.busy; i += 1) {
  const chunk = await shell.output({ since: seen });
  if (chunk.length) { process.stdout.write(chunk); seen += chunk.length; }
  await new Promise((r) => setTimeout(r, 500));
}
```

## Piped, not a terminal emulator {#pty}

| Works | Does not |
| --- | --- |
| Ordinary commands, build tools, servers | Full-screen interfaces that redraw |
| Ctrl+C, through interrupt() | Interactive prompts that need a real terminal |
| cwd that follows cd | Colours and cursor control, in general |

A program that insists on a terminal will say so or behave oddly. Prefer the non-interactive flags such tools almost always have.

## A computer only {#throws}

Anything else raises `storage_not_local` — including a phone, which has no shell of its own. A page can be given one through [the shell bridge](/sdk/agent/bridges/shell), and then an agent has `bash` there too.

- [bash](/sdk/agent/tools/bash): How an agent runs a command, and what it is asked before.
- [terminal](/sdk/agent/tools/terminal): The agent's own view of these — free to read, permissioned to kill.

```python
pc = api.storage.get(id=machine_id)

shell = pc.terminals.create(name="dev", cwd="myapp")

shell.run("npm run dev")
shell.output(since=0)   # what it has printed so far

shell.busy   # whether the shell is still running
shell.cwd    # the folder it was started in

shell.interrupt()       # Ctrl+C
shell.kill()
```

```python
if shell.busy:
    shell.interrupt()     # or wait, but do not just send
shell.run("npm test")
```

```python
import sys, time

seen = 0

for _ in range(60):
    if not shell.busy:
        break
    chunk = shell.output(since=seen)
    if chunk["text"]:
        sys.stdout.write(chunk["text"])
    seen = chunk["cursor"]
    time.sleep(0.5)
```
