Skip to content

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 command
const response = await sandbox.process.executeCommand('ls -la');
console.log(response.result);
// Run TypeScript code
const response = await sandbox.process.codeRun('console.log("Hello, World!")');
console.log(response.result);
// Using interactive sessions
// Create a new session
const sessionId = 'my-session';
await sandbox.process.createSession(sessionId);
// Execute commands in the session
const response = await sandbox.process.executeSessionCommand(sessionId, {
command: 'cd /workspace'
});
const response2 = await sandbox.process.executeSessionCommand(sessionId, {
command: 'pwd'
});
console.log(response2.result); // Should print "/workspace"
// Clean up
await sandbox.process.deleteSession(sessionId);

CodeRunParams

Parameters for code execution

Constructors

new CodeRunParams()

new CodeRunParams(): CodeRunParams
Returns

CodeRunParams

Properties

PropertyTypeDescription
argv?string[]Command line arguments
env?Record<string, string>Environment variables

Process

Constructors

new Process()

new Process(
codeToolbox: SandboxCodeToolbox,
toolboxApi: ToolboxApi,
instance: SandboxInstance): Process
Parameters
ParameterType
codeToolboxSandboxCodeToolbox
toolboxApiToolboxApi
instanceSandboxInstance
Returns

Process

Methods

codeRun()

codeRun(
code: string,
params?: CodeRunParams,
timeout?: number): Promise<ExecuteResponse>

Executes code in the Sandbox using the appropriate language runtime.

Parameters
ParameterTypeDescription
codestringCode to execute
params?CodeRunParamsParameters for code execution
timeout?number-
Returns

Promise<ExecuteResponse>

Code execution results containing:

  • exitCode: The execution’s exit status
  • result: Standard output from the code
Example
// Run TypeScript code
const 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
ParameterTypeDescription
sessionIdstringUnique identifier for the new session
Returns

Promise<void>

Example
// Create a new session
const 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
ParameterTypeDescription
sessionIdstringUnique identifier of the session to delete
Returns

Promise<void>

Example
// Clean up a completed session
await process.deleteSession('my-session');

executeCommand()

executeCommand(
command: string,
cwd?: string,
timeout?: number): Promise<ExecuteResponse>

Executes a shell command in the Sandbox.

Parameters
ParameterTypeDescription
commandstringShell command to execute
cwd?stringWorking directory for command execution. If not specified, uses the Sandbox root directory
timeout?numberMaximum 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 command
const response = await process.executeCommand('echo "Hello"');
console.log(response.result); // Prints: Hello
// Command with working directory
const result = await process.executeCommand('ls', '/workspace/src');
// Command with timeout
const result = await process.executeCommand('sleep 10', undefined, 5);

executeSessionCommand()

executeSessionCommand(
sessionId: string,
req: SessionExecuteRequest,
timeout?: number): Promise<SessionExecuteResponse>

Executes a command in an existing session.

Parameters
ParameterTypeDescription
sessionIdstringUnique identifier of the session to use
reqSessionExecuteRequestCommand execution request containing: - command: The command to execute - async: Whether to execute asynchronously
timeout?numberTimeout in seconds
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 state
const sessionId = 'my-session';
// Change directory
await process.executeSessionCommand(sessionId, {
command: 'cd /workspace'
});
// Run command in new directory
const result = await process.executeSessionCommand(sessionId, {
command: 'pwd'
});
console.log(result.output); // Prints: /workspace

getSession()

getSession(sessionId: string): Promise<Session>

Get a session in the sandbox.

Parameters
ParameterTypeDescription
sessionIdstringUnique 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
ParameterTypeDescription
sessionIdstringUnique identifier of the session
commandIdstringUnique 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()

Call Signature
getSessionCommandLogs(sessionId: string, commandId: string): Promise<string>

Get the logs for a command executed in a session.

Parameters
ParameterTypeDescription
sessionIdstringUnique identifier of the session
commandIdstringUnique identifier of the command
Returns

Promise<string>

Command logs

Example
const logs = await process.getSessionCommandLogs('my-session', 'cmd-123');
console.log('Command output:', logs);
Call Signature
getSessionCommandLogs(
sessionId: string,
commandId: string,
onLogs: (chunk: string) => void
): Promise<void>

Asynchronously retrieve and process the logs for a command executed in a session as they become available.

Parameters
ParameterTypeDescription
sessionIdstringUnique identifier of the session
commandIdstringUnique identifier of the command
onLogs(chunk: string) => voidCallback function to handle each log chunk
Returns

Promise<void>

Example
const logs = await process.getSessionCommandLogs('my-session', 'cmd-123', (chunk) => {
console.log('Log chunk:', chunk);
});

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})`);
});
});