|
| 1 | +--- |
| 2 | +title: Commands |
| 3 | +pcx_content_type: concept |
| 4 | +sidebar: |
| 5 | + order: 1 |
| 6 | +--- |
| 7 | + |
| 8 | +import { Details, TypeScriptExample } from "~/components"; |
| 9 | + |
| 10 | +Execute commands and manage background processes in the sandbox's isolated container environment. |
| 11 | + |
| 12 | +## Methods |
| 13 | + |
| 14 | +### `exec()` |
| 15 | + |
| 16 | +Execute a command and return the complete result. |
| 17 | + |
| 18 | +```ts |
| 19 | +const result = await sandbox.exec(command: string, options?: ExecOptions): Promise<ExecuteResponse> |
| 20 | +``` |
| 21 | + |
| 22 | +**Parameters**: |
| 23 | +- `command` - The command to execute (can include arguments) |
| 24 | +- `options` (optional): |
| 25 | + - `stream` - Enable streaming callbacks (default: `false`) |
| 26 | + - `onOutput` - Callback for real-time output: `(stream: 'stdout' | 'stderr', data: string) => void` |
| 27 | + - `timeout` - Maximum execution time in milliseconds |
| 28 | + |
| 29 | +**Returns**: `Promise<ExecuteResponse>` with `success`, `stdout`, `stderr`, `exitCode` |
| 30 | + |
| 31 | +<TypeScriptExample> |
| 32 | +``` |
| 33 | +const result = await sandbox.exec('npm run build'); |
| 34 | +
|
| 35 | +if (result.success) { |
| 36 | + console.log('Build output:', result.stdout); |
| 37 | +} else { |
| 38 | + console.error('Build failed:', result.stderr); |
| 39 | +} |
| 40 | +
|
| 41 | +// With streaming |
| 42 | +await sandbox.exec('npm install', { |
| 43 | + stream: true, |
| 44 | + onOutput: (stream, data) => console.log(`[${stream}] ${data}`) |
| 45 | +}); |
| 46 | +``` |
| 47 | +</TypeScriptExample> |
| 48 | + |
| 49 | +### `execStream()` |
| 50 | + |
| 51 | +Execute a command and return a Server-Sent Events stream for real-time processing. |
| 52 | + |
| 53 | +```ts |
| 54 | +const stream = await sandbox.execStream(command: string, options?: ExecOptions): Promise<ReadableStream> |
| 55 | +``` |
| 56 | + |
| 57 | +**Parameters**: |
| 58 | +- `command` - The command to execute |
| 59 | +- `options` - Same as `exec()` |
| 60 | + |
| 61 | +**Returns**: `Promise<ReadableStream>` emitting `ExecEvent` objects (`start`, `stdout`, `stderr`, `complete`, `error`) |
| 62 | + |
| 63 | +<TypeScriptExample> |
| 64 | +``` |
| 65 | +import { parseSSEStream, type ExecEvent } from '@cloudflare/sandbox'; |
| 66 | +
|
| 67 | +const stream = await sandbox.execStream('npm run build'); |
| 68 | +
|
| 69 | +for await (const event of parseSSEStream<ExecEvent>(stream)) { |
| 70 | + switch (event.type) { |
| 71 | + case 'stdout': |
| 72 | + console.log('Output:', event.data); |
| 73 | + break; |
| 74 | + case 'complete': |
| 75 | + console.log('Exit code:', event.exitCode); |
| 76 | + break; |
| 77 | + case 'error': |
| 78 | + console.error('Failed:', event.error); |
| 79 | + break; |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | +</TypeScriptExample> |
| 84 | + |
| 85 | +### `startProcess()` |
| 86 | + |
| 87 | +Start a long-running background process. |
| 88 | + |
| 89 | +```ts |
| 90 | +const process = await sandbox.startProcess(command: string, options?: ProcessOptions): Promise<ProcessInfo> |
| 91 | +``` |
| 92 | + |
| 93 | +**Parameters**: |
| 94 | +- `command` - The command to start as a background process |
| 95 | +- `options` (optional): |
| 96 | + - `cwd` - Working directory |
| 97 | + - `env` - Environment variables |
| 98 | + |
| 99 | +**Returns**: `Promise<ProcessInfo>` with `id`, `pid`, `command`, `status` |
| 100 | + |
| 101 | +<TypeScriptExample> |
| 102 | +``` |
| 103 | +const server = await sandbox.startProcess('python -m http.server 8000'); |
| 104 | +console.log('Started with PID:', server.pid); |
| 105 | +
|
| 106 | +// With custom environment |
| 107 | +const app = await sandbox.startProcess('node app.js', { |
| 108 | + cwd: '/workspace/my-app', |
| 109 | + env: { NODE_ENV: 'production', PORT: '3000' } |
| 110 | +}); |
| 111 | +``` |
| 112 | +</TypeScriptExample> |
| 113 | + |
| 114 | +### `listProcesses()` |
| 115 | + |
| 116 | +List all running processes. |
| 117 | + |
| 118 | +```ts |
| 119 | +const processes = await sandbox.listProcesses(): Promise<ProcessInfo[]> |
| 120 | +``` |
| 121 | + |
| 122 | +<TypeScriptExample> |
| 123 | +``` |
| 124 | +const processes = await sandbox.listProcesses(); |
| 125 | +
|
| 126 | +for (const proc of processes) { |
| 127 | + console.log(`${proc.id}: ${proc.command} (PID ${proc.pid})`); |
| 128 | +} |
| 129 | +``` |
| 130 | +</TypeScriptExample> |
| 131 | + |
| 132 | +### `killProcess()` |
| 133 | + |
| 134 | +Terminate a specific process. |
| 135 | + |
| 136 | +```ts |
| 137 | +await sandbox.killProcess(processId: string, signal?: string): Promise<void> |
| 138 | +``` |
| 139 | + |
| 140 | +**Parameters**: |
| 141 | +- `processId` - The process ID (from `startProcess()` or `listProcesses()`) |
| 142 | +- `signal` - Signal to send (default: `"SIGTERM"`) |
| 143 | + |
| 144 | +<TypeScriptExample> |
| 145 | +``` |
| 146 | +const server = await sandbox.startProcess('python -m http.server 8000'); |
| 147 | +await sandbox.killProcess(server.id); |
| 148 | +``` |
| 149 | +</TypeScriptExample> |
| 150 | + |
| 151 | +### `killAllProcesses()` |
| 152 | + |
| 153 | +Terminate all running processes. |
| 154 | + |
| 155 | +```ts |
| 156 | +await sandbox.killAllProcesses(): Promise<void> |
| 157 | +``` |
| 158 | + |
| 159 | +<TypeScriptExample> |
| 160 | +``` |
| 161 | +await sandbox.killAllProcesses(); |
| 162 | +``` |
| 163 | +</TypeScriptExample> |
| 164 | + |
| 165 | +### `streamProcessLogs()` |
| 166 | + |
| 167 | +Stream logs from a running process in real-time. |
| 168 | + |
| 169 | +```ts |
| 170 | +const stream = await sandbox.streamProcessLogs(processId: string): Promise<ReadableStream> |
| 171 | +``` |
| 172 | + |
| 173 | +**Parameters**: |
| 174 | +- `processId` - The process ID |
| 175 | + |
| 176 | +**Returns**: `Promise<ReadableStream>` emitting `LogEvent` objects |
| 177 | + |
| 178 | +<TypeScriptExample> |
| 179 | +``` |
| 180 | +import { parseSSEStream, type LogEvent } from '@cloudflare/sandbox'; |
| 181 | +
|
| 182 | +const server = await sandbox.startProcess('node server.js'); |
| 183 | +const logStream = await sandbox.streamProcessLogs(server.id); |
| 184 | +
|
| 185 | +for await (const log of parseSSEStream<LogEvent>(logStream)) { |
| 186 | + console.log(`[${log.timestamp}] ${log.data}`); |
| 187 | +
|
| 188 | + if (log.data.includes('Server started')) break; |
| 189 | +} |
| 190 | +``` |
| 191 | +</TypeScriptExample> |
| 192 | + |
| 193 | +### `getProcessLogs()` |
| 194 | + |
| 195 | +Get accumulated logs from a process. |
| 196 | + |
| 197 | +```ts |
| 198 | +const logs = await sandbox.getProcessLogs(processId: string): Promise<string> |
| 199 | +``` |
| 200 | + |
| 201 | +**Parameters**: |
| 202 | +- `processId` - The process ID |
| 203 | + |
| 204 | +**Returns**: `Promise<string>` with all accumulated output |
| 205 | + |
| 206 | +<TypeScriptExample> |
| 207 | +``` |
| 208 | +const server = await sandbox.startProcess('node server.js'); |
| 209 | +await new Promise(resolve => setTimeout(resolve, 5000)); |
| 210 | +
|
| 211 | +const logs = await sandbox.getProcessLogs(server.id); |
| 212 | +console.log('Server logs:', logs); |
| 213 | +``` |
| 214 | +</TypeScriptExample> |
| 215 | + |
| 216 | +## Related resources |
| 217 | + |
| 218 | +- [Background processes guide](/sandbox/guides/background-processes/) - Managing long-running processes |
| 219 | +- [Files API](/sandbox/api/files/) - File operations |
0 commit comments