Start long-running services, stream their output, send signals, and manage process lifecycles inside sandboxes.
Process operations use the sandbox proxy URL: https://<sandbox-id-or-name>.sandbox.tensorlake.ai Named sandboxes can use the sandbox name in place of the ID in the proxy hostname. The process APIs documented here run on the management URL on port 9501, which always requires authentication. Unauthenticated proxy access applies only to exposed user ports.
Start a Background Process
Python
TypeScript
CLI
HTTP
from tensorlake.sandbox import SandboxClient
client = SandboxClient()
with client.create_and_connect() as sandbox:
# Start a background process
proc = sandbox.start_process( "python" , [ "-m" , "http.server" , "8080" ])
print ( f "PID: { proc.pid } " )
const proc = await sandbox . startProcess ( "python" , {
args: [ "-m" , "http.server" , "8080" ],
});
console . log ( `PID: ${ proc . pid } ` );
# Run a command in the background using shell syntax
tl sbx exec < sandbox-i d > bash -c "python -m http.server 8080 &"
curl -X POST https:// < sandbox-i d > .sandbox.tensorlake.ai/api/v1/processes \
-H "Authorization: Bearer $TL_API_KEY " \
-H "Content-Type: application/json" \
-d '{
"command": "python",
"args": ["-m", "http.server", "8080"]
}'
Response (201 Created):{
"pid" : 42 ,
"status" : "running" ,
"command" : "python" ,
"args" : [ "-m" , "http.server" , "8080" ],
"stdin_writable" : false ,
"started_at" : 1710000000000
}
Request options: {
"command" : "python" ,
"args" : [ "-m" , "http.server" , "8080" ],
"env" : { "PORT" : "8080" },
"working_dir" : "/workspace" ,
"stdin_mode" : "pipe" ,
"stdout_mode" : "capture" ,
"stderr_mode" : "capture"
}
List Processes
# List is available via the process manager
procs = sandbox.list_processes()
for p in procs:
print ( f "PID { p.pid } : { p.status } " )
const processes = await sandbox . listProcesses ();
for ( const proc of processes ) {
console . log ( `PID ${ proc . pid } : ${ proc . status } ` );
}
curl https:// < sandbox-i d > .sandbox.tensorlake.ai/api/v1/processes \
-H "Authorization: Bearer $TL_API_KEY "
Response: {
"processes" : [
{
"pid" : 42 ,
"status" : "running" ,
"command" : "python" ,
"args" : [ "-m" , "http.server" , "8080" ],
"stdin_writable" : false ,
"started_at" : 1710000000000
}
]
}
Stream Process Output
Monitor process output in real time as it produces stdout/stderr:
with client.create_and_connect() as sandbox:
proc = sandbox.start_process( "python" , [ "-c" , """
import time
for i in range(10):
print(f"Processing item {i+1}/10")
time.sleep(1)
""" ])
# Stream output line by line
for event in sandbox.follow_output(proc.pid):
print (event.line, end = "" )
const proc = await sandbox . startProcess ( "python" , {
args: [
"-c" ,
"import time \n for i in range(3): \n print(f'Processing item {i+1}/3') \n time.sleep(1)" ,
],
});
for await ( const event of sandbox . followOutput ( proc . pid )) {
console . log ( event . line );
}
# Follow combined output via SSE
curl -N https:// < sandbox-i d > .sandbox.tensorlake.ai/api/v1/processes/ < pi d > /output/follow \
-H "Authorization: Bearer $TL_API_KEY "
SSE stream: event: output
data: {"line":"Processing item 1/10\n","timestamp":1710000000000,"stream":"stdout"}
event: output
data: {"line":"Processing item 2/10\n","timestamp":1710000001000,"stream":"stdout"}
event: eof
data: {}
Send Signals
Send POSIX signals to running processes:
import signal
with client.create_and_connect() as sandbox:
proc = sandbox.start_process( "python" , [ "-m" , "http.server" , "8080" ])
# Gracefully stop the process
sandbox.send_signal(proc.pid, signal. SIGTERM )
await sandbox . sendSignal ( proc . pid , 15 );
# Send SIGTERM (15)
curl -X POST https:// < sandbox-i d > .sandbox.tensorlake.ai/api/v1/processes/ < pi d > /signal \
-H "Authorization: Bearer $TL_API_KEY " \
-H "Content-Type: application/json" \
-d '{"signal": 15}'
# Send SIGKILL (9)
curl -X POST https:// < sandbox-i d > .sandbox.tensorlake.ai/api/v1/processes/ < pi d > /signal \
-H "Authorization: Bearer $TL_API_KEY " \
-H "Content-Type: application/json" \
-d '{"signal": 9}'
Kill a Process
sandbox.send_signal(proc.pid, signal. SIGKILL )
await sandbox . killProcess ( proc . pid );
curl -X DELETE https:// < sandbox-i d > .sandbox.tensorlake.ai/api/v1/processes/ < pi d > \
-H "Authorization: Bearer $TL_API_KEY "
Write to Stdin
Send input to a running process with stdin in pipe mode:
import { StdinMode } from "tensorlake" ;
const proc = await sandbox . startProcess ( "python" , {
args: [ "-i" ],
stdinMode: StdinMode . PIPE ,
});
await sandbox . writeStdin (
proc . pid ,
new TextEncoder (). encode ( "print('hello') \n " ),
);
await sandbox . closeStdin ( proc . pid );
# Start a process with stdin pipe
curl -X POST https:// < sandbox-i d > .sandbox.tensorlake.ai/api/v1/processes \
-H "Authorization: Bearer $TL_API_KEY " \
-H "Content-Type: application/json" \
-d '{"command": "python", "args": ["-i"], "stdin_mode": "pipe"}'
# Write to stdin
curl -X POST https:// < sandbox-i d > .sandbox.tensorlake.ai/api/v1/processes/ < pi d > /stdin \
-H "Authorization: Bearer $TL_API_KEY " \
-H "Content-Type: application/octet-stream" \
--data-binary "print('hello')\n"
# Close stdin
curl -X POST https:// < sandbox-i d > .sandbox.tensorlake.ai/api/v1/processes/ < pi d > /stdin/close \
-H "Authorization: Bearer $TL_API_KEY "
Learn More
Commands Execute commands in sandboxes.
Lifecycle Sandbox states, resources, and timeouts.
Networking Control internet access and outbound destinations.