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

# 启动子进程

使用 [`Bun.spawn()`](/runtime/child-process) 启动子进程。

```ts theme={"theme":{"light":"github-light","dark":"dracula"}}
const proc = Bun.spawn(["echo", "hello"]);

// 等待完成
await proc.exited;
```

***

第二个参数接受一个配置对象。

```ts theme={"theme":{"light":"github-light","dark":"dracula"}}
const proc = Bun.spawn(["echo", "Hello, world!"], {
  cwd: "/tmp",
  env: { FOO: "bar" },
  onExit(proc, exitCode, signalCode, error) {
    // 退出处理
  },
});
```

***

默认情况下，子进程的 `stdout` 可以通过 `proc.stdout` 作为 `ReadableStream` 来读取。

```ts theme={"theme":{"light":"github-light","dark":"dracula"}}
const proc = Bun.spawn(["echo", "hello"]);

const output = await proc.stdout.text();
output; // => "hello\n"
```

***

完整文档请参见 [文档 > API > 子进程](/runtime/child-process)。
