用户与工作目录

更新时间:
复制 MD 格式

模板中的用户和工作目录会影响命令执行、文件权限和依赖安装位置。Agent 场景中,建议保持工作目录稳定,避免每次执行命令都依赖隐式路径。

查看当前用户和目录

const result = await sandbox.commands.run("whoami && pwd");
console.log(result.stdout);

Python 示例:

result = sandbox.commands.run("whoami && pwd")
print(result.stdout)

指定命令工作目录

await sandbox.files.makeDir("/tmp/project");
await sandbox.files.write("/tmp/project/main.py", "print('hello')\n");

const result = await sandbox.commands.run("python3 main.py", {
  cwd: "/tmp/project",
});

console.log(result.stdout.trim());

Python 示例:

sandbox.files.make_dir("/tmp/project")
sandbox.files.write("/tmp/project/main.py", "print('hello')\n")

result = sandbox.commands.run(
    "python3 main.py",
    cwd="/tmp/project",
)

print(result.stdout.strip())

使用建议

  • 模板构建时固定常用工具和依赖。

  • 任务运行时使用明确的 cwd

  • 不要假设所有镜像都有相同默认用户、HOME 目录和权限。