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

# Interactive shells and TTY

> Use interactive TTY sessions with shell() for terminal applications and raw byte streaming.

이 예시에서는 `CWSANDBOX_API_KEY`에 설정된 CoreWeave API 액세스 토큰을 사용합니다. Forge API 키 인증은 [시작하기](/ko/products/sandboxes/get-started)를 참조하세요. CKS 요구 사항은 [배치 모드](/ko/products/sandboxes/placement)를 참조하세요.

대화형 TTY 세션에는 `Sandbox.shell()`을 사용하세요. 이 메서드는 대화형 사용을 위해 설계된 `TerminalSession`을 반환하며, 출력 버퍼링 없이 원시 바이트 스트리밍을 제공합니다. 일괄 명령어 실행에는 대신 [`exec()`](/ko/products/sandboxes/client/guides/execution)을 사용하세요.

<h2 id="shell-session">
  셸 세션
</h2>

샌드박스를 생성하고 대화형 셸 세션을 엽니다. 인수 없이 `Sandbox.run()`을 호출하면 기본 장기 실행 명령어를 사용합니다. 이후 예시는 이 컨텍스트가 여전히 활성 상태라고 가정합니다.

```python theme={"system"}
from cwsandbox import Sandbox

with Sandbox.run() as sandbox:
    terminal = sandbox.shell(
        ["/bin/bash"],
        width=80,
        height=24,
    )
```

`shell()`은 항상 TTY를 할당하고 stdin을 활성화합니다. 반환된 `TerminalSession`은 UTF-8 디코딩이나 인코딩을 거치지 않고 원시 바이트를 스트림하므로 터미널 이스케이프 시퀀스와 바이너리 출력을 안전하게 처리할 수 있습니다.

<h2 id="send-commands-and-read-output">
  명령어 전송 및 출력 조회
</h2>

터미널 세션이 열린 상태에서 stdin에 명령어를 쓰고 원격 셸에서 병합된 바이트 출력을 스트림으로 받아오세요.

```python theme={"system"}
import sys

terminal.stdin.writeline("echo hello").result()
terminal.stdin.writeline("ls -la /tmp").result()
terminal.stdin.writeline("exit").result()

# 원시 바이트 출력을 순회합니다
for chunk in terminal.output:
    sys.stdout.buffer.write(chunk)
    sys.stdout.buffer.flush()  # 세션 실행 중 출력이 표시되도록 플러시합니다.

# 종료 코드를 조회합니다
exit_code = terminal.wait(timeout=5.0)
```

`TerminalSession`은 출력을 버퍼링하지 않습니다. stdout과 stderr는 하나의 원시 바이트 스트림으로 병합되어 `terminal.output`으로 전달됩니다. 결과에는 별도의 `stdout` 또는 `stderr` 속성이 없습니다. `terminal.output`을 계속해서 조회하세요. 조회를 중단하면 크기가 제한된 큐에서 백프레셔가 발생하여 원격 세션이 멈춥니다.

<h2 id="terminal-resize">
  터미널 크기 조정
</h2>

터미널 크기가 변경될 때 크기 조정 메시지를 전송하세요.

```python theme={"system"}
terminal.resize(120, 40)
```

`resize()`는 `TerminalSession`에서 fire-and-forget입니다.

<h2 id="exit-a-shell-session">
  셸 세션 종료
</h2>

stdin을 닫거나 exit 명령어를 전송하여 셸 세션을 종료하세요:

```python theme={"system"}
terminal.stdin.writeline("exit").result()  # 원격 셸에 종료를 요청하세요
# 또는
terminal.stdin.close().result()            # stdin 스트림을 닫으세요
```

원격 프로세스가 종료되면 셸 세션이 완료됩니다. 종료 코드가 반환될 때까지 대기하려면 `terminal.wait()` 또는 `terminal.result()`를 호출하세요.

<h2 id="commands-and-use-cases">
  명령어 및 사용 사례
</h2>

| 사용 사례 | 접근 방식 |
| - | - |
| 터미널에서 일회성 명령어 | `cwsandbox exec [ID] echo hello` |
| 터미널에서 대화형 셸 | `cwsandbox sh [ID]` |
| 스크립트 실행, 출력 캡처하다 (SDK) | `exec(["python", "script.py"])` |
| 대화형 셸 세션 (SDK) | `shell(["/bin/bash"])` |
| 명령어에 입력 전송 (SDK) | `exec(["cat"], stdin=True)` |
| 터미널 애플리케이션 (vim, htop) | `shell(["vim"])` + CLI raw mode |

<h2 id="see-also">
  참고 자료
</h2>

* [명령어 실행](/ko/products/sandboxes/client/guides/execution) - `exec()`로 명령어를 실행하세요.
* [샌드박스 로깅](/ko/products/sandboxes/client/guides/logging) - 컨테이너 로그를 스트리밍하세요.
