diff --git a/src/content/docs/sandbox/api/commands.mdx b/src/content/docs/sandbox/api/commands.mdx index c8aacaea1d39c0..57548778fa5e8a 100644 --- a/src/content/docs/sandbox/api/commands.mdx +++ b/src/content/docs/sandbox/api/commands.mdx @@ -140,7 +140,7 @@ for (const proc of processes) { ### `killProcess()` -Terminate a specific process. +Terminate a specific process and all of its child processes. ```ts await sandbox.killProcess(processId: string, signal?: string): Promise @@ -150,10 +150,17 @@ await sandbox.killProcess(processId: string, signal?: string): Promise - `processId` - The process ID (from `startProcess()` or `listProcesses()`) - `signal` - Signal to send (default: `"SIGTERM"`) +Sends the signal to the entire process group, ensuring that both the main process and any child processes it spawned are terminated. This prevents orphaned processes from continuing to run after the parent is killed. + ``` const server = await sandbox.startProcess('python -m http.server 8000'); await sandbox.killProcess(server.id); + +// Example with a process that spawns children +const script = await sandbox.startProcess('bash -c "sleep 10 & sleep 10 & wait"'); +// killProcess terminates both sleep commands and the bash process +await sandbox.killProcess(script.id); ``` diff --git a/src/content/docs/sandbox/guides/background-processes.mdx b/src/content/docs/sandbox/guides/background-processes.mdx index a523eba829da40..4c7c178fc15563 100644 --- a/src/content/docs/sandbox/guides/background-processes.mdx +++ b/src/content/docs/sandbox/guides/background-processes.mdx @@ -140,9 +140,11 @@ console.log('Logs:', logs); ## Stop processes +Stop background processes and their children: + ``` -// Stop specific process +// Stop specific process (terminates entire process tree) await sandbox.killProcess(server.id); // Force kill if needed @@ -153,6 +155,22 @@ await sandbox.killAllProcesses(); ``` +`killProcess()` terminates the specified process and all child processes it spawned. This ensures that processes running in the background do not leave orphaned child processes when terminated. + +For example, if your process spawns multiple worker processes or background tasks, `killProcess()` will clean up the entire process tree: + + +``` +// This script spawns multiple child processes +const batch = await sandbox.startProcess( + 'bash -c "process1 & process2 & process3 & wait"' +); + +// killProcess() terminates the bash process AND all three child processes +await sandbox.killProcess(batch.id); +``` + + ## Run multiple processes Start services in sequence, waiting for dependencies: