method
Spawn.SpawnSyncOptions.onExit
Callback that runs when the Subprocess exits
This is called even if the process exits with a non-zero exit code.
Warning: this may run before the Bun.spawn function returns.
An alternative is await subprocess.exited.
If an error occurred in the call to waitpid2, this is the error.
const subprocess = spawn({
cmd: ["echo", "hello"],
onExit: (subprocess, code) => {
console.log(`Process exited with code ${code}`);
},
});Referenced types
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.
The 3 optional type parameters correspond to the stdio array from the options object. Instead of specifying them, use one of these utility types:
- 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
nullif the process hasn't exited yet - readonly exited: Promise<number>
The exit code of the process
The promise resolves when the process exits
- 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>
The same value as Subprocess.stdout
Exists for compatibility with ReadableStream.pipeThrough
- readonly signalCode: null | Signals
Synchronously get the signal code of the process
nullif the process never sent a signal codeTo receive signal code changes, use the
onExitcallback.If the signal code is unknown, this is the original signal code number, but that case should never happen in practice.
- readonly stdio: [null, null, null, ...null | number[]]
Extra file descriptors passed to the
stdiooption.Entries beyond index 2 are
numberfor"pipe"and"socket-fd"slots and, on POSIX, for slots where a raw file descriptor was supplied (the same fd is returned). On POSIX, reading this property transfers ownership of any"pipe"fds to the caller, who is then responsible for closing them; the subprocess will not close them."socket-fd"and raw-fd slots are likewise caller-owned. Other slots — including raw fds on Windows — arenull. - readonly terminal: undefined | Terminal
The terminal attached to this subprocess, if spawned with the
terminaloption.undefinedif no terminal was attached.When a terminal is attached,
stdin,stdout, andstderrreturnnull. Useterminal.write()and thedatacallback instead.const proc = Bun.spawn(["bash"], { terminal: { data: (term, data) => console.log(data.toString()) }, }); proc.terminal?.write("echo hello\n"); Disconnect the IPC channel to the subprocess. This is only supported if the subprocess was created with the
ipcoption.- @param exitCode
Exit code or signal to send to the process
Tell Bun to wait for this process to exit after you already called
unref().By default, Bun waits for all subprocesses to exit before shutting down
Get the resource usage of the process, such as max RSS and CPU time
Returns
undefineduntil the process has exited- send(message: any): void;
Send a message to the subprocess. This is only supported if the subprocess was created with the
ipcoption, and is another instance ofbun.Messages are serialized using the JSC serialize API, which allows for the same types that
postMessage/structuredClonesupports. Tell Bun not to wait for this process to exit before shutting down.
By default, Bun waits for all subprocesses to exit before shutting down.