Process and Code Execution
The Daytona SDK provides powerful process and code execution capabilities through
the process
module in Sandboxes. This guide covers all available process operations
and best practices.
Examples
// Execute a shell commandconst response = await workspace.process.executeCommand('ls -la');console.log(response.result);
// Run TypeScript codeconst response = await workspace.process.codeRun('console.log("Hello, World!")');console.log(response.result);
// Using interactive sessions// Create a new sessionconst sessionId = 'my-session';await workspace.process.createSession(sessionId);
// Execute commands in the sessionconst response = await workspace.process.executeSessionCommand(sessionId, { command: 'cd /workspace'});
const response2 = await workspace.process.executeSessionCommand(sessionId, { command: 'pwd'});console.log(response2.result); // Should print "/workspace"
// Clean upawait workspace.process.deleteSession(sessionId);
Process
Constructors
new Process()
new Process( codeToolbox: WorkspaceCodeToolbox, toolboxApi: ToolboxApi, instance: Workspace): Process
Parameters
Parameter | Type |
---|---|
codeToolbox | WorkspaceCodeToolbox |
toolboxApi | ToolboxApi |
instance | Workspace |
Returns
Process
Methods
codeRun()
codeRun(code: string): Promise<ExecuteResponse>
Executes code in the Sandbox using the appropriate language runtime.
Parameters
Parameter | Type | Description |
---|---|---|
code | string | Code to execute |
Returns
Promise<ExecuteResponse>
Code execution results containing:
- exitCode: The execution’s exit status
- result: Standard output from the code
Example
// Run TypeScript codeconst response = await process.codeRun(` const x = 10; const y = 20; console.log(\`Sum: \${x + y}\`);`);console.log(response.result); // Prints: Sum: 30
createSession()
createSession(sessionId: string): Promise<void>
Creates a new long-running background session in the Sandbox.
Sessions are background processes that maintain state between commands, making them ideal for scenarios requiring multiple related commands or persistent environment setup. You can run long-running commands and monitor process status.
Parameters
Parameter | Type | Description |
---|---|---|
sessionId | string | Unique identifier for the new session |
Returns
Promise<void>
Example
// Create a new sessionconst sessionId = 'my-session';await process.createSession(sessionId);const session = await process.getSession(sessionId);// Do work...await process.deleteSession(sessionId);
deleteSession()
deleteSession(sessionId: string): Promise<void>
Delete a session from the Sandbox.
Parameters
Parameter | Type | Description |
---|---|---|
sessionId | string | Unique identifier of the session to delete |
Returns
Promise<void>
Example
// Clean up a completed sessionawait process.deleteSession('my-session');
executeCommand()
executeCommand( command: string, cwd?: string,timeout?: number): Promise<ExecuteResponse>
Executes a shell command in the Sandbox.
Parameters
Parameter | Type | Description |
---|---|---|
command | string | Shell command to execute |
cwd? | string | Working directory for command execution. If not specified, uses the Sandbox root directory |
timeout? | number | Maximum time in seconds to wait for the command to complete. 0 means wait indefinitely. |
Returns
Promise<ExecuteResponse>
Command execution results containing:
- exitCode: The command’s exit status
- result: Standard output from the command
Examples
// Simple commandconst response = await process.executeCommand('echo "Hello"');console.log(response.result); // Prints: Hello
// Command with working directoryconst result = await process.executeCommand('ls', '/workspace/src');
// Command with timeoutconst result = await process.executeCommand('sleep 10', undefined, 5);
executeSessionCommand()
executeSessionCommand(sessionId: string, req: SessionExecuteRequest): Promise<SessionExecuteResponse>
Executes a command in an existing session.
Parameters
Parameter | Type | Description |
---|---|---|
sessionId | string | Unique identifier of the session to use |
req | SessionExecuteRequest | Command execution request containing: - command: The command to execute - async: Whether to execute asynchronously |
Returns
Promise<SessionExecuteResponse>
Command execution results containing:
- cmdId: Unique identifier for the executed command
- output: Command output (if synchronous execution)
- exitCode: Command exit status (if synchronous execution)
Example
// Execute commands in sequence, maintaining stateconst sessionId = 'my-session';
// Change directoryawait process.executeSessionCommand(sessionId, { command: 'cd /workspace'});
// Run command in new directoryconst result = await process.executeSessionCommand(sessionId, { command: 'pwd'});console.log(result.output); // Prints: /workspace
getSession()
getSession(sessionId: string): Promise<Session>
Get a session in the workspace.
Parameters
Parameter | Type | Description |
---|---|---|
sessionId | string | Unique identifier of the session to retrieve |
Returns
Promise<Session>
Session information including:
- sessionId: The session’s unique identifier
- commands: List of commands executed in the session
Example
const session = await process.getSession('my-session');session.commands.forEach(cmd => { console.log(`Command: ${cmd.command}`);});
getSessionCommand()
getSessionCommand(sessionId: string, commandId: string): Promise<Command>
Gets information about a specific command executed in a session.
Parameters
Parameter | Type | Description |
---|---|---|
sessionId | string | Unique identifier of the session |
commandId | string | Unique identifier of the command |
Returns
Promise<Command>
Command information including:
- id: The command’s unique identifier
- command: The executed command string
- exitCode: Command’s exit status (if completed)
Example
const cmd = await process.getSessionCommand('my-session', 'cmd-123');if (cmd.exitCode === 0) { console.log(`Command ${cmd.command} completed successfully`);}
getSessionCommandLogs()
getSessionCommandLogs(sessionId: string, commandId: string): Promise<string>
Get the logs for a command executed in a session.
Parameters
Parameter | Type | Description |
---|---|---|
sessionId | string | Unique identifier of the session |
commandId | string | Unique identifier of the command |
Returns
Promise<string>
Command logs
Example
const logs = await process.getSessionCommandLogs('my-session', 'cmd-123');console.log('Command output:', logs);
listSessions()
listSessions(): Promise<Session[]>
Lists all active sessions in the Sandbox.
Returns
Promise<Session[]>
Array of active sessions
Example
const sessions = await process.listSessions();sessions.forEach(session => { console.log(`Session ${session.sessionId}:`); session.commands.forEach(cmd => { console.log(`- ${cmd.command} (${cmd.exitCode})`); });});