终端命令
支持在沙箱内执行 Shell 命令并获取标准输出等结果。
支持函数
| 函数名称 | 说明 |
|---|---|
| sandbox.commands.run(cmd) | 执行命令,返回结果对象,含 stdout、stderr 等 |
| sandbox.commands.kill(pid) | 使用 SIGKILL 终止正在运行的命令;返回布尔值:ture为终止成功、false为未找到命令 |
| sandbox.commands.connect(pid) | 连接到运行中的命令,返回 CommandHadle句柄,用于等待命令完成、获取执行结果、发送输入等交互 |
使用示例
from e2b_code_interpreter import Sandbox
# 创建
sandbox = Sandbox.create(template="sandbox1")
# 执行命令
result = sandbox.commands.run("ls -l")
result = sandbox.commands.run("echo Hello, E2B! > test_echo.txt")
result = sandbox.commands.run("cat test_echo.txt")
assert "Hello, E2B!" in result.stdout