# Terminals

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

`storage.terminals → LocalTerminals`

Conchas e servidores in un computer che sopraviva a una sola risposta. Un servidor dev iniziou aqui sta ancora in esecuzione nel próximo appello.

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

Un comando ergue e esperat muere con la sua risposta. Un servèrve dev ha bisogno del contrario: incessa, la lascia, torna a sa output.

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

## La superficie {#surface}

| Chamada | Função |
| --- | --- |
| terminals.list() | Quals s'operò. |
| terminals.get(id) | Uno di loro. |
| terminals.create({ name, cwd, command }) | Abre un shell, optionalmente a rular algo in una volta. |
| terminals.remove(id) | - Close uno. |
| terminal.run(line) | Envia un comando. |
| terminal.output({ since }) | Ce ha imprimit, da un offset. |
| terminal.interrupt() | Ctrl+C — ferma il comando, mantiene la shell. |
| terminal.kill() | Fine la shell. |

## Viven nel processo che esegue il SDK {#process}

> **Reiniciare il programma e ogni terminale s'è partito**
>
> Un servèrder dev inceput a travers uno muore quando il processo che lo sostene usca — incluso su una recarga in modo watch.

Due conseguenze vale la pena disegnare: un second instancia del vostro app ha i propri terminali e non può vedre i primi, e un terminal non è un modo di mantenere in funzione qualcosa dopo che si è fatto.

## Nunca scrivi a un guscio che è occupat {#busy}

Un shell esegue un comandi per volta. L'emissione di una riga mentre uno corre spinge testo in quello che lee stdin — che è un prompt, un editor, o nullo.

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

## Lectura di output come viene {#output}

`output({ since })` restituisce ciò che è stato impresso da un offset, quindi il sondaggio è un buco con un cursore in movimento, invece di un releit de tutto.

```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, non un emulador terminal {#pty}

| Operàs | Non fa |
| --- | --- |
| Comandos ordinari, build tools, servers | Interfaces a schermo completo che redesen |
| Ctrl+C, mediante interrupt() | Provoca interattivo che necessita di un terminale real |
| cwd che segue cd | Colori e control del cursor, in general |

Un programma che insiste su un terminale dicir o comportar curi. Preferir le bandies non interattive tali utensili quasi sempre ha.

## Un computer solamente {#throws}

Qualcosa di altre cose suscita `storage_not_local` — compreso un telefono, che non ha shell propria. Una pagina può essere dat uno a través [o ponte shell](/pt/sdk/agent/bridges/shell), e poi un agent ha `bash` là.

- [ball](/pt/sdk/agent/tools/bash): Como un agente executa un comando, e ce que si chiedeu ante.
- [terminale](/pt/sdk/agent/tools/terminal): La propria veduta del agente di questi — libere da ler, permese per a uccider.

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