Subprocess

Bun

Symbol

Subprocess

interface Subprocess<In extends SpawnOptions.Writable = SpawnOptions.Writable, Out extends SpawnOptions.Readable = SpawnOptions.Readable, Err extends SpawnOptions.Readable = SpawnOptions.Readable>

A process created by Bun.spawn.

This type accepts 3 optional type parameters which correspond to the stdio array from the options object. Instead of specifying these, you should use one of the following utility types instead:

  • ReadableSubprocess (any, pipe, pipe)
  • WritableSubprocess (pipe, any, any)
  • PipedSubprocess (pipe, pipe, pipe)
  • NullSubprocess (ignore, ignore, ignore)
  • readonly exitCode: null | number

    Synchronously get the exit code of the process

    If the process hasn't exited yet, this will return null

  • readonly exited: Promise<number>

    The exit code of the process

    The promise will resolve when the process exits

  • readonly killed: boolean

    Has the process exited?

  • readonly pid: number

    The process ID of the child process

    const { pid } = Bun.spawn({ cmd: ["echo", "hello"] });
    console.log(pid); // 1234
    
  • readonly readable: ReadableToIO<Out>

    This returns the same value as Subprocess.stdout

    It exists for compatibility with ReadableStream.pipeThrough

  • readonly signalCode: null | Signals

    Synchronously get the signal code of the process

    If the process never sent a signal code, this will return null

    To receive signal code changes, use the onExit callback.

    If the signal code is unknown, it will return the original signal code number, but that case should essentially never happen.

  • readonly stderr: ReadableToIO<Err>
  • readonly stdin: WritableToIO<In>
  • readonly stdout: ReadableToIO<Out>
  • [Symbol.asyncDispose](): PromiseLike<void>
  • disconnect(): void

    Disconnect the IPC channel to the subprocess. This is only supported if the subprocess was created with the ipc option.

  • kill(exitCode?: number | Signals): void

    Kill the process

    @param exitCode

    The exitCode to send to the process

  • ref(): void

    This method will tell Bun to wait for this process to exit after you already called unref().

    Before shutting down, Bun will wait for all subprocesses to exit by default

  • Get the resource usage information of the process (max RSS, CPU time, etc)

    Only available after the process has exited

    If the process hasn't exited yet, this will return undefined

  • send(message: any): void

    Send a message to the subprocess. This is only supported if the subprocess was created with the ipc option, and is another instance of bun.

    Messages are serialized using the JSC serialize API, which allows for the same types that postMessage/structuredClone supports.

  • unref(): void

    Before shutting down, Bun will wait for all subprocesses to exit by default

    This method will tell Bun to not wait for this process to exit before shutting down.