mirror of
https://github.com/cloudflare/cloudflare-docs.git
synced 2026-01-16 23:11:06 +00:00
Document killProcess() now terminates child processes
Update documentation to reflect that killProcess() now properly terminates the entire process tree including child processes, preventing orphaned processes from continuing to run after the parent is killed. Changes: - Update killProcess() API reference to clarify it kills process groups - Add example showing child process termination behavior - Update background processes guide with detailed explanation - Add example demonstrating process tree cleanup Syncs documentation with cloudflare/sandbox-sdk#339
This commit is contained in:
parent
0b7af691f6
commit
ecd71d1c03
2 changed files with 27 additions and 2 deletions
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue