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

Process

[view_source]

Constructors

new Process()

new Process(
codeToolbox: WorkspaceCodeToolbox,
toolboxApi: ToolboxApi,
instance: Workspace): Process

[view_source]

Parameters
ParameterType
codeToolboxWorkspaceCodeToolbox
toolboxApiToolboxApi
instanceWorkspace
Returns

Process

Methods

codeRun()

codeRun(code: string): Promise<ExecuteResponse>

[view_source]

Executes code in the Sandbox using the appropriate language runtime.

Parameters
ParameterTypeDescription
codestringCode to execute
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>

[view_source]

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>

[view_source]

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>

[view_source]

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): Promise<SessionExecuteResponse>

[view_source]

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
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>

[view_source]

Get a session in the workspace.

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>

[view_source]

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()

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

[view_source]

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

listSessions()

listSessions(): Promise<Session[]>

[view_source]

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