Start and manage long-running background processes in sandboxes with real-time log streaming and lifecycle control.
Background processes let you run long-running commands that continue executing while your Worker handles other requests. Perfect for web servers, build tools, data processing jobs, and monitoring tasks.
completed - Process exited successfully (exit code 0)
failed - Process exited with non-zero code
killed - Process was terminated by signal
error - Process failed to start
const process = await sandbox.startProcess('npm run build');// Check current statusconst status = await process.getStatus();console.log(`Process is ${status}`);// Wait for completionconst result = await process.waitForExit();console.log(`Process exited with code ${result.exitCode}`);
When starting servers, wait for them to be ready before connecting:
// Start a web serverconst server = await sandbox.startProcess( 'python3 -m http.server 8000');// Wait for port to be ready (HTTP check)await server.waitForPort(8000);console.log('Server is ready!');// Now you can expose the portconst exposed = await sandbox.exposePort(8000);console.log(`Access server at: ${exposed.url}`);
const server = await sandbox.startProcess( 'node server.js');// Wait for "Server listening" messageconst result = await server.waitForLog(/Server listening on port (\d+)/);console.log('Found:', result.line);console.log('Port:', result.match?.[1]);
// Simple string matchawait process.waitForLog('Ready to accept connections');// Regex with capture groupsconst result = await process.waitForLog( /Database initialized in (\d+)ms/);console.log(`Init time: ${result.match?.[1]}ms`);// With timeouttry { await process.waitForLog('Started', 10000);} catch (error) { console.error('Process did not start within 10 seconds');}
const process = await sandbox.startProcess('node server.js', { processId: 'web-server', // Custom ID for later reference cwd: '/workspace/app', // Working directory env: { PORT: '8000', NODE_ENV: 'production' }, timeout: 300000, // Kill after 5 minutes autoCleanup: false // Keep process record after exit});
// Start dev server with hot reloadconst server = await sandbox.startProcess( 'npm run dev', { processId: 'dev-server' });// Wait for it to be readyawait server.waitForLog('Local: http');// Expose the portawait sandbox.exposePort(5173);