Process and Code Execution
Daytona provides process and code execution capabilities through the process module in sandboxes.
Code execution
Daytona provides methods to execute code in sandboxes. You can run code snippets in multiple languages with support for both stateless execution and stateful interpretation with persistent contexts.
Run code (stateless)
Daytona provides methods to run code snippets in sandboxes using stateless execution. Each invocation starts from a clean interpreter, making it ideal for independent code snippets.
# Run Python coderesponse = sandbox.process.code_run('''def greet(name): return f"Hello, {name}!"
print(greet("Daytona"))''')
print(response.result)// Run TypeScript codelet response = await sandbox.process.codeRun(`function greet(name: string): string { return \`Hello, \${name}!\`;}
console.log(greet("Daytona"));`);console.log(response.result);
// Run code with argv and environment variablesresponse = await sandbox.process.codeRun( ` console.log(\`Hello, \${process.argv[2]}!\`); console.log(\`FOO: \${process.env.FOO}\`); `, { argv: ["Daytona"], env: { FOO: "BAR" } });console.log(response.result);
// Run code with timeout (5 seconds)response = await sandbox.process.codeRun( 'setTimeout(() => console.log("Done"), 2000);', undefined, 5);console.log(response.result);# Run Python coderesponse = sandbox.process.code_run(code: <<~PYTHON) def greet(name): return f"Hello, {name}!"
print(greet("Daytona"))PYTHON
puts response.result// Run code using shell command execution// Note: For stateless code execution in Go, use ExecuteCommand with the appropriate interpreterresult, err := sandbox.Process.ExecuteCommand(ctx, `python3 -c 'def greet(name): return f"Hello, {name}!"
print(greet("Daytona"))'`)if err != nil { log.Fatal(err)}fmt.Println(result.Result)
// Run code with environment variablesresult, err = sandbox.Process.ExecuteCommand(ctx, `python3 -c 'import os; print(f"FOO: {os.environ.get(\"FOO\")}")'`, options.WithCommandEnv(map[string]string{"FOO": "BAR"}),)if err != nil { log.Fatal(err)}fmt.Println(result.Result)
// Run code with timeoutresult, err = sandbox.Process.ExecuteCommand(ctx, `python3 -c 'import time; time.sleep(2); print("Done")'`, options.WithExecuteTimeout(5*time.Second),)if err != nil { log.Fatal(err)}fmt.Println(result.Result)curl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/code-run' \ --request POST \ --header 'Content-Type: application/json' \ --data '{ "code": "def greet(name):\n return f\"Hello, {name}!\"\n\nprint(greet(\"Daytona\"))", "env": { "FOO": "BAR" }, "timeout": 5000}'For more information, see the Python SDK, TypeScript SDK, Ruby SDK, Go SDK, and API references:
Run code (stateful)
Daytona provides methods to run code with persistent state using the code interpreter. You can maintain variables and imports between calls, create isolated contexts, and control environment variables.
from daytona import Daytona, OutputMessage
def handle_stdout(message: OutputMessage): print(f"[STDOUT] {message.output}")
daytona = Daytona()sandbox = daytona.create()
# Shared default contextresult = sandbox.code_interpreter.run_code( "counter = 1\nprint(f'Counter initialized at {counter}')", on_stdout=handle_stdout,)
# Isolated contextctx = sandbox.code_interpreter.create_context()try: sandbox.code_interpreter.run_code( "value = 'stored in ctx'", context=ctx, ) sandbox.code_interpreter.run_code( "print(value)", context=ctx, on_stdout=handle_stdout, )finally: sandbox.code_interpreter.delete_context(ctx)import { Daytona } from '@daytonaio/sdk'
const daytona = new Daytona()
async function main() { const sandbox = await daytona.create()
// Shared default context await sandbox.codeInterpreter.runCode(`counter = 1print(f'Counter initialized at {counter}')`, { onStdout: (msg) => process.stdout.write(`[STDOUT] ${msg.output}`)}, )
// Isolated context const ctx = await sandbox.codeInterpreter.createContext() try { await sandbox.codeInterpreter.runCode( `value = 'stored in ctx'`, { context: ctx }, ) await sandbox.codeInterpreter.runCode( `print(value)`, { context: ctx, onStdout: (msg) => process.stdout.write(`[STDOUT] ${msg.output}`) }, ) } finally { await sandbox.codeInterpreter.deleteContext(ctx) }}
main()# Ruby SDK uses process.code_run for code execution# Stateful contexts are managed through the code interpreter API
require 'daytona'
daytona = Daytona::Daytona.newsandbox = daytona.create
# Run code (stateless in Ruby SDK)response = sandbox.process.code_run(code: <<~PYTHON) counter = 1 print(f'Counter initialized at {counter}')PYTHON
puts response.result// Create a code interpreter contextctxInfo, err := sandbox.CodeInterpreter.CreateContext(ctx, nil)if err != nil { log.Fatal(err)}contextID := ctxInfo["id"].(string)
// Run code in the contextchannels, err := sandbox.CodeInterpreter.RunCode(ctx, "counter = 1\nprint(f'Counter initialized at {counter}')", options.WithCustomContext(contextID),)if err != nil { log.Fatal(err)}
// Read outputfor msg := range channels.Stdout { fmt.Printf("[STDOUT] %s\n", msg.Text)}
// Clean up contexterr = sandbox.CodeInterpreter.DeleteContext(ctx, contextID)if err != nil { log.Fatal(err)}# Create contextcurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/interpreter/context' \ --request POST \ --header 'Content-Type: application/json' \ --data '{}'
# Run code in context (WebSocket endpoint)# Connect via WebSocket to:# wss://proxy.app.daytona.io/toolbox/{sandboxId}/process/interpreter/execute# Send JSON message:# {# "code": "counter = 1\nprint(f\"Counter initialized at {counter}\")",# "contextId": "your-context-id"# }
# Delete contextcurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/interpreter/context/{contextId}' \ --request DELETEFor more information, see the Python SDK, TypeScript SDK, Ruby SDK, Go SDK, and API references:
createContext (TypeScript SDK)
Command execution
Daytona provides methods to execute shell commands in sandboxes. You can run commands with working directory, timeout, and environment variable options.
The working directory defaults to the sandbox working directory. It uses the WORKDIR specified in the Dockerfile if present, or falls back to the user’s home directory if not (e.g., workspace/repo implies /home/daytona/workspace/repo). You can override it with an absolute path by starting the path with /.
Execute commands
Daytona provides methods to execute shell commands in sandboxes by providing the command string and optional parameters for working directory, timeout, and environment variables. You can also use the daytona exec CLI command for quick command execution.
# Execute any shell commandresponse = sandbox.process.exec("ls -la")print(response.result)
# Setting a working directory and a timeout
response = sandbox.process.exec("sleep 3", cwd="workspace/src", timeout=5)print(response.result)
# Passing environment variables
response = sandbox.process.exec("echo $CUSTOM_SECRET", env={ "CUSTOM_SECRET": "DAYTONA" })print(response.result)// Execute any shell commandconst response = await sandbox.process.executeCommand("ls -la");console.log(response.result);
// Setting a working directory and a timeoutconst response2 = await sandbox.process.executeCommand("sleep 3", "workspace/src", undefined, 5);console.log(response2.result);
// Passing environment variablesconst response3 = await sandbox.process.executeCommand("echo $CUSTOM_SECRET", ".", { "CUSTOM_SECRET": "DAYTONA" });console.log(response3.result);# Execute any shell commandresponse = sandbox.process.exec(command: 'ls -la')puts response.result
# Setting a working directory and a timeoutresponse = sandbox.process.exec(command: 'sleep 3', cwd: 'workspace/src', timeout: 5)puts response.result
# Passing environment variablesresponse = sandbox.process.exec( command: 'echo $CUSTOM_SECRET', env: { 'CUSTOM_SECRET' => 'DAYTONA' })puts response.result// Execute any shell commandresponse, err := sandbox.Process.ExecuteCommand(ctx, "ls -la")if err != nil { log.Fatal(err)}fmt.Println(response.Result)
// Setting a working directory and a timeoutresponse, err = sandbox.Process.ExecuteCommand(ctx, "sleep 3", options.WithCwd("workspace/src"), options.WithExecuteTimeout(5*time.Second),)if err != nil { log.Fatal(err)}fmt.Println(response.Result)
// Passing environment variablesresponse, err = sandbox.Process.ExecuteCommand(ctx, "echo $CUSTOM_SECRET", options.WithCommandEnv(map[string]string{"CUSTOM_SECRET": "DAYTONA"}),)if err != nil { log.Fatal(err)}fmt.Println(response.Result)# Execute any shell commanddaytona exec my-sandbox -- ls -la
# Setting a working directory and a timeoutdaytona exec my-sandbox --cwd workspace/src --timeout 5 -- sleep 3
# Passing environment variables (use shell syntax)daytona exec my-sandbox -- sh -c 'CUSTOM_SECRET=DAYTONA echo $CUSTOM_SECRET'curl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/execute' \ --request POST \ --header 'Content-Type: application/json' \ --data '{ "command": "ls -la", "cwd": "workspace", "timeout": 5}'For more information, see the Python SDK, TypeScript SDK, Ruby SDK, Go SDK, CLI, and API references:
Session operations
Daytona provides methods to manage background process sessions in sandboxes. You can create sessions, execute commands, monitor status, and manage long-running processes.
Get session status
Daytona provides methods to get session status and list all sessions in a sandbox by providing the session ID.
# Check session's executed commandssession = sandbox.process.get_session(session_id)print(f"Session {session_id}:")for command in session.commands: print(f"Command: {command.command}, Exit Code: {command.exit_code}")
# List all running sessions
sessions = sandbox.process.list_sessions()for session in sessions: print(f"Session: {session.session_id}, Commands: {session.commands}")// Check session's executed commandsconst session = await sandbox.process.getSession(sessionId);console.log(`Session ${sessionId}:`);for (const command of session.commands) { console.log(`Command: ${command.command}, Exit Code: ${command.exitCode}`);}
// List all running sessionsconst sessions = await sandbox.process.listSessions();for (const session of sessions) { console.log(`Session: ${session.sessionId}, Commands: ${session.commands}`);}# Check session's executed commandssession = sandbox.process.get_session(session_id)puts "Session #{session_id}:"session.commands.each do |command| puts "Command: #{command.command}, Exit Code: #{command.exit_code}"end
# List all running sessionssessions = sandbox.process.list_sessionssessions.each do |session| puts "Session: #{session.session_id}, Commands: #{session.commands}"end// Check session's executed commandssession, err := sandbox.Process.GetSession(ctx, sessionID)if err != nil { log.Fatal(err)}fmt.Printf("Session %s:\n", sessionID)commands := session["commands"].([]any)for _, cmd := range commands { cmdMap := cmd.(map[string]any) fmt.Printf("Command: %s, Exit Code: %v\n", cmdMap["command"], cmdMap["exitCode"])}
// List all running sessionssessions, err := sandbox.Process.ListSessions(ctx)if err != nil { log.Fatal(err)}for _, sess := range sessions { fmt.Printf("Session: %s, Commands: %v\n", sess["sessionId"], sess["commands"])}# Get session infocurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/session/{sessionId}'
# List all sessionscurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/session'For more information, see the Python SDK, TypeScript SDK, Ruby SDK, Go SDK, and API references:
Execute interactive commands
Daytona provides methods to execute interactive commands in sessions. You can send input to running commands that expect user interaction, such as confirmations or interactive tools like database CLIs and package managers.
session_id = "interactive-session"sandbox.process.create_session(session_id)
# Execute command that requires confirmationcommand = sandbox.process.execute_session_command( session_id, SessionExecuteRequest( command='pip uninstall requests', run_async=True, ),)
# Stream logs asynchronouslylogs_task = asyncio.create_task( sandbox.process.get_session_command_logs_async( session_id, command.cmd_id, lambda log: print(f"[STDOUT]: {log}"), lambda log: print(f"[STDERR]: {log}"), ))
await asyncio.sleep(1)# Send input to the commandsandbox.process.send_session_command_input(session_id, command.cmd_id, "y")
# Wait for logs to completeawait logs_taskconst sessionId = 'interactive-session'await sandbox.process.createSession(sessionId)
// Execute command that requires confirmationconst command = await sandbox.process.executeSessionCommand(sessionId, { command: 'pip uninstall requests', runAsync: true,})
// Stream logs asynchronouslyconst logPromise = sandbox.process.getSessionCommandLogs( sessionId, command.cmdId!, (stdout) => console.log('[STDOUT]:', stdout), (stderr) => console.log('[STDERR]:', stderr),)
await new Promise((resolve) => setTimeout(resolve, 1000))// Send input to the commandawait sandbox.process.sendSessionCommandInput(sessionId, command.cmdId!, 'y')
// Wait for logs to completeawait logPromisesession_id = "interactive-session"sandbox.process.create_session(session_id)
# Execute command that requires confirmationinteractive_command = sandbox.process.execute_session_command( session_id: session_id, req: Daytona::SessionExecuteRequest.new( command: 'pip uninstall requests', run_async: true ))
# Wait a moment for the command to startsleep 1
# Send input to the commandsandbox.process.send_session_command_input( session_id: session_id, command_id: interactive_command.cmd_id, data: "y")
# Get logs for the interactive command asynchronouslysandbox.process.get_session_command_logs_async( session_id: session_id, command_id: interactive_command.cmd_id, on_stdout: ->(log) { puts "[STDOUT]: #{log}" }, on_stderr: ->(log) { puts "[STDERR]: #{log}" })sessionID := "interactive-session"err := sandbox.Process.CreateSession(ctx, sessionID)if err != nil { log.Fatal(err)}
// Execute command that requires confirmationresult, err := sandbox.Process.ExecuteSessionCommand(ctx, sessionID, "pip uninstall requests", true)if err != nil { log.Fatal(err)}cmdID := result["cmdId"].(string)
// Stream logs asynchronouslystdout := make(chan string)stderr := make(chan string)
go func() { err := sandbox.Process.GetSessionCommandLogsStream(ctx, sessionID, cmdID, stdout, stderr) if err != nil { log.Println("Log stream error:", err) }}()
time.Sleep(1 * time.Second)
// Note: SendSessionCommandInput is not available in Go SDK// Use the API endpoint directly for sending input
// Read logsfor msg := range stdout { fmt.Printf("[STDOUT]: %s\n", msg)}# Create sessioncurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/session' \ --request POST \ --header 'Content-Type: application/json' \ --data '{"sessionId": "interactive-session"}'
# Execute session commandcurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/session/{sessionId}/exec' \ --request POST \ --header 'Content-Type: application/json' \ --data '{ "command": "pip uninstall requests", "runAsync": true}'
# Send input to commandcurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/session/{sessionId}/command/{commandId}/input' \ --request POST \ --header 'Content-Type: application/json' \ --data '{ "data": "y"}'
# Get command logscurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/session/{sessionId}/command/{commandId}/logs'For more information, see the Python SDK, TypeScript SDK, Ruby SDK, Go SDK, and API references:
execute_session_command (Python SDK)
send_session_command_input (Python SDK)
get_session_command_logs_async (Python SDK)
createSession (TypeScript SDK)
executeSessionCommand (TypeScript SDK)
sendSessionCommandInput (TypeScript SDK)
getSessionCommandLogs (TypeScript SDK)
execute_session_command (Ruby SDK)
send_session_command_input (Ruby SDK)
ExecuteSessionCommand (Go SDK)
Resource management
Daytona provides methods to manage session resources. You should use sessions for long-running operations, clean up sessions after execution, and handle exceptions properly.
# Python - Clean up sessionsession_id = "long-running-cmd"try: sandbox.process.create_session(session_id) session = sandbox.process.get_session(session_id) # Do work...finally: sandbox.process.delete_session(session.session_id)// TypeScript - Clean up sessionconst sessionId = "long-running-cmd";try { await sandbox.process.createSession(sessionId); const session = await sandbox.process.getSession(sessionId); // Do work...} finally { await sandbox.process.deleteSession(session.sessionId);}# Ruby - Clean up sessionsession_id = 'long-running-cmd'begin sandbox.process.create_session(session_id) session = sandbox.process.get_session(session_id) # Do work...ensure sandbox.process.delete_session(session.session_id)end// Go - Clean up sessionsessionID := "long-running-cmd"err := sandbox.Process.CreateSession(ctx, sessionID)if err != nil { log.Fatal(err)}defer sandbox.Process.DeleteSession(ctx, sessionID)
session, err := sandbox.Process.GetSession(ctx, sessionID)if err != nil { log.Fatal(err)}// Do work...# Create sessioncurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/session' \ --request POST \ --header 'Content-Type: application/json' \ --data '{"sessionId": "long-running-cmd"}'
# Delete session when donecurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/session/{sessionId}' \ --request DELETEFor more information, see the Python SDK, TypeScript SDK, Ruby SDK, Go SDK, and API references:
createSession (TypeScript SDK)
Error handling
Daytona provides methods to handle errors when executing processes. You should handle process exceptions properly, log error details for debugging, and use try-catch blocks for error handling.
from daytona import DaytonaError
try: response = sandbox.process.code_run("invalid python code") if response.exit_code != 0: print(f"Exit code: {response.exit_code}") print(f"Error output: {response.result}")except DaytonaError as e: print(f"Execution failed: {e}")import { DaytonaError } from '@daytonaio/sdk'
try { const response = await sandbox.process.codeRun("invalid typescript code"); if (response.exitCode !== 0) { console.error("Exit code:", response.exitCode); console.error("Error output:", response.result); }} catch (e) { if (e instanceof DaytonaError) { console.error("Execution failed:", e); }}begin response = sandbox.process.code_run(code: 'invalid python code') if response.exit_code != 0 puts "Exit code: #{response.exit_code}" puts "Error output: #{response.result}" endrescue StandardError => e puts "Execution failed: #{e}"endresult, err := sandbox.Process.ExecuteCommand(ctx, "python3 -c 'invalid python code'")if err != nil { fmt.Println("Execution failed:", err)}if result != nil && result.ExitCode != 0 { fmt.Println("Exit code:", result.ExitCode) fmt.Println("Error output:", result.Result)}# API responses include exitCode field for error handlingcurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/execute' \ --request POST \ --header 'Content-Type: application/json' \ --data '{ "command": "python3 -c \"invalid python code\""}'
# Response includes:# {# "result": "",# "exitCode": 1# }For more information, see the Python SDK, TypeScript SDK, Ruby SDK, and Go SDK references.
Common issues
Daytona provides solutions for troubleshooting common issues related to process and code execution.
| Issue | Solutions |
|---|---|
| Process execution failed | • Check command syntax • Verify required dependencies • Ensure sufficient permissions |
| Process timeout | • Adjust timeout settings • Optimize long-running operations • Consider using background processes |
| Resource limits | • Monitor process memory usage • Handle process cleanup properly • Use appropriate resource constraints |