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.
A simple alternative is await subprocess.exited
.
Symbol
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.
A simple alternative is await subprocess.exited
.
If an error occurred in the call to waitpid2, this will be the error.
const subprocess = spawn({
cmd: ["echo", "hello"],
onExit: (subprocess, code) => {
console.log(`Process exited with code ${code}`);
},
});
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:
Synchronously get the exit code of the process
If the process hasn't exited yet, this will return null
The exit code of the process
The promise will resolve when the process exits
The process ID of the child process
const { pid } = Bun.spawn({ cmd: ["echo", "hello"] });
console.log(pid); // 1234
This returns the same value as Subprocess.stdout
It exists for compatibility with ReadableStream.pipeThrough
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.
Disconnect the IPC channel to the subprocess. This is only supported if the subprocess was created with the ipc
option.
Kill the process
The exitCode to send to the process
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 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.
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.