Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/content/docs/sandbox/api/commands.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>
Expand All @@ -150,10 +150,17 @@ await sandbox.killProcess(processId: string, signal?: string): Promise<void>
- `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.

<TypeScriptExample>
```
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);
```
</TypeScriptExample>

Expand Down
20 changes: 19 additions & 1 deletion src/content/docs/sandbox/guides/background-processes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,11 @@ console.log('Logs:', logs);

## Stop processes

Stop background processes and their children:

<TypeScriptExample>
```
// Stop specific process
// Stop specific process (terminates entire process tree)
await sandbox.killProcess(server.id);

// Force kill if needed
Expand All @@ -153,6 +155,22 @@ await sandbox.killAllProcesses();
```
</TypeScriptExample>

`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:

<TypeScriptExample>
```
// 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);
```
</TypeScriptExample>

## Run multiple processes

Start services in sequence, waiting for dependencies:
Expand Down
Loading