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
46 changes: 46 additions & 0 deletions src/content/docs/sandbox/api/commands.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ const process = await sandbox.startProcess(command: string, options?: ProcessOpt
- `getLogs()` - Get accumulated logs
- `waitForPort()` - Wait for process to listen on a port
- `waitForLog()` - Wait for pattern in process output
- `waitForExit()` - Wait for process to exit and get exit code

<TypeScriptExample>
```
Expand Down Expand Up @@ -316,6 +317,51 @@ await server.waitForLog('Ready', 30000);
- `ProcessReadyTimeoutError` - If pattern is not found within timeout
- `ProcessExitedBeforeReadyError` - If process exits before pattern appears

### `process.waitForExit()`

Wait for a process to exit and retrieve its exit code.

```ts
const result = await process.waitForExit(timeout?: number): Promise<WaitForExitResult>
```

**Parameters**:
- `timeout` - Maximum wait time in milliseconds (optional)

**Returns**: `Promise<WaitForExitResult>` with:
- `exitCode` - The process exit code (0 for success, non-zero for errors)

<TypeScriptExample>
```
const build = await sandbox.startProcess('npm run build');

// Wait for build to complete
const result = await build.waitForExit();

if (result.exitCode === 0) {
console.log('Build succeeded');
} else {
console.error('Build failed with exit code:', result.exitCode);

// Get logs to see what went wrong
const logs = await build.getLogs();
console.error('Build logs:', logs);
}

// With timeout
const test = await sandbox.startProcess('npm test');
try {
const result = await test.waitForExit(60000); // 60 second timeout
console.log('Tests completed with exit code:', result.exitCode);
} catch (error) {
console.error('Tests timed out after 60 seconds');
}
```
</TypeScriptExample>

**Throws**:
- `ProcessReadyTimeoutError` - If process does not exit within timeout

## Related resources

- [Background processes guide](/sandbox/guides/background-processes/) - Managing long-running processes
Expand Down
44 changes: 44 additions & 0 deletions src/content/docs/sandbox/guides/background-processes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,49 @@ console.log('Server is ready:', result.line);
```
</TypeScriptExample>

## Wait for process completion

For short-lived processes like builds or tests, wait for the process to exit and check the exit code:

<TypeScriptExample>
```
const build = await sandbox.startProcess('npm run build');

// Wait for build to complete
const result = await build.waitForExit();

if (result.exitCode === 0) {
console.log('Build succeeded');

// Get the output
const logs = await sandbox.getProcessLogs(build.id);
console.log('Build output:', logs);
} else {
console.error('Build failed with exit code:', result.exitCode);
}
```
</TypeScriptExample>

With a timeout to prevent waiting indefinitely:

<TypeScriptExample>
```
const test = await sandbox.startProcess('npm test');

try {
// Wait up to 60 seconds for tests to complete
const result = await test.waitForExit(60000);

console.log('Tests completed with exit code:', result.exitCode);
} catch (error) {
if (error.code === 'PROCESS_READY_TIMEOUT') {
console.error('Tests timed out after 60 seconds');
await sandbox.killProcess(test.id);
}
}
```
</TypeScriptExample>

## Monitor process logs

Stream logs in real-time:
Expand Down Expand Up @@ -226,6 +269,7 @@ When using `keepAlive: true`, containers will not automatically timeout. You **m
## Best practices

- **Wait for readiness** - Use `waitForPort()` or `waitForLog()` to detect when services are ready
- **Wait for completion** - Use `waitForExit()` for build processes and scripts to check exit codes
- **Clean up** - Always stop processes when done
- **Handle failures** - Monitor logs for errors and restart if needed
- **Use try/finally** - Ensure cleanup happens even on errors
Expand Down
Loading