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

# 衍生子进程

使用 [`Bun.spawn()`](/runtime/child-process) 来衍生子进程。

```ts theme={null}
const proc = Bun.spawn(["echo", "hello"]);

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

***

第二个参数是一个配置对象。

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

***

默认情况下，`proc.stdout` 是一个包含子进程 `stdout` 的 `ReadableStream`。

```ts theme={null}
const proc = Bun.spawn(["echo", "hello"]);

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

***

参见 [子进程](/runtime/child-process)。
