Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Add docs examples for stdout/err and on exit

Jakub Novakf05c84bd2ae5
diff --git a/apps/docs/src/app/playgrounds/process/page.mdx b/apps/docs/src/app/playgrounds/process/page.mdx
index b07c7f172..d1cc9aaa2 100644
--- a/apps/docs/src/app/playgrounds/process/page.mdx
+++ b/apps/docs/src/app/playgrounds/process/page.mdx
@@ -8,6 +8,7 @@ export const sections = [
{title: 'Stop process', id: 'stop-process'},
{title: 'Stream stdout', id: 'stream-stdout'},
{title: 'Stream stderr', id: 'stream-stderr'},
+ {title: 'On process exit', id: 'on-process-exit'},
{title: 'Send stdin', id: 'send-stdin'},
{title: 'Set environment variables', id: 'set-environment-variables'},
]
@@ -21,6 +22,7 @@ Here are the basic operations you can do with the process inside a cloud playgro
- [Stdout](/playgrounds/process#stream-stdout)
- [Stdin](/playgrounds/process#send-stdin)
- [Stderr](/playgrounds/process#stream-stderr)
+- [On exit](/playgrounds/process#on-process-exit)
- [Environment variables](/playgrounds/process#set-environment-variables)
## Start process
@@ -39,6 +41,8 @@ Here are the basic operations you can do with the process inside a cloud playgro
## Stream stdout
+Set either stdout handler for whole session level or per process.
+
<CodeGroupAutoload
path="basics/process_stream_stdout"
isRunnable={false}
@@ -46,11 +50,22 @@ Here are the basic operations you can do with the process inside a cloud playgro
## Stream stderr
+Set either stderr handler for whole session level or per process.
+
<CodeGroupAutoload
path="basics/process_stream_stderr"
isRunnable={false}
/>
+## On process exit
+
+Set either on exit handler for whole session level or per process.
+
+<CodeGroupAutoload
+ path="basics/process_exit"
+ isRunnable={false}
+/>
+
## Send stdin
<CodeGroupAutoload
diff --git a/apps/docs/src/code/js/basics/process_exit.js b/apps/docs/src/code/js/basics/process_exit.js
new file mode 100644
index 000000000..29148f182
--- /dev/null
+++ b/apps/docs/src/code/js/basics/process_exit.js
@@ -0,0 +1,20 @@
+import { Session } from '@e2b/sdk'
+
+const session = await Session.create({
+ id: 'Nodejs',
+ apiKey: process.env.E2B_API_KEY,
+ onExit: () => console.log('[session]', 'process ended'), // $HighlightLine
+})
+
+const proc = await session.process.start({ cmd: 'echo "Hello World!"' })
+await proc.finished
+// output: [session] process ended
+
+const procWithCustomHandler = await session.process.start({
+ cmd: 'echo "Hello World!"',
+ onExit: () => console.log('[process]', 'process ended'), // $HighlightLine
+})
+await procWithCustomHandler.finished
+// output: [process] process ended
+
+await session.close()
diff --git a/apps/docs/src/code/js/basics/process_stream_stderr.js b/apps/docs/src/code/js/basics/process_stream_stderr.js
index aa19b39af..0ceb6edfb 100644
--- a/apps/docs/src/code/js/basics/process_stream_stderr.js
+++ b/apps/docs/src/code/js/basics/process_stream_stderr.js
@@ -8,8 +8,16 @@ const session = await Session.create({
// This command will fail and output to stderr because Golang isn't installed in the cloud playground
const golangVersion = await session.process.start({
cmd: 'go version',
- onStderr: output => console.log(output), // $HighlightLine
+ onStderr: output => console.log('session', output.line), // $HighlightLine
})
await golangVersion.finished
+// output: [session] /bin/bash: line 1: go: command not found
+
+const procWithCustomHandler = await session.process.start({
+ cmd: 'go version',
+ onStderr: data => console.log('process', data.line), // $HighlightLine
+})
+await procWithCustomHandler.finished
+// output: process /bin/bash: line 1: go: command not found
await session.close()
diff --git a/apps/docs/src/code/js/basics/process_stream_stdout.js b/apps/docs/src/code/js/basics/process_stream_stdout.js
index 7b8abc1aa..5b1a225a6 100644
--- a/apps/docs/src/code/js/basics/process_stream_stdout.js
+++ b/apps/docs/src/code/js/basics/process_stream_stdout.js
@@ -3,12 +3,20 @@ import { Session } from '@e2b/sdk'
const session = await Session.create({
id: 'Nodejs',
apiKey: process.env.E2B_API_KEY,
+ onStdout: output => console.log('session', output.line), // $HighlightLine
})
-const npmInit = await session.process.start({
- cmd: 'npm init -y',
- onStdout: output => console.log(output), // $HighlightLine
+const proc = await session.process.start({
+ cmd: 'echo "Hello World!"',
})
-await npmInit.finished
+await proc.finished
+// output: session Hello World!
+
+const procWithCustomHandler = await session.process.start({
+ cmd: 'echo "Hello World!"',
+ onStdout: data => console.log('process', data.line), // $HighlightLine
+})
+await procWithCustomHandler.finished
+// output: process Hello World!
await session.close()
diff --git a/apps/docs/src/code/python/basics/process_exit.py b/apps/docs/src/code/python/basics/process_exit.py
new file mode 100644
index 000000000..360e1463a
--- /dev/null
+++ b/apps/docs/src/code/python/basics/process_exit.py
@@ -0,0 +1,27 @@
+import asyncio
+from os import getenv
+from e2b import Session
+
+E2B_API_KEY = getenv("E2B_API_KEY")
+
+async def main():
+ session = await Session.create(
+ id="Nodejs",
+ api_key=E2B_API_KEY,
+ on_exit=lambda: print("[session]", "process ended"), # $HighlightLine
+ )
+
+ proc = await session.process.start('echo "Hello World!"')
+ await proc
+ # output: [session] process ended
+
+ proc_with_custom_handler = await session.process.start(
+ 'echo "Hello World!"',
+ on_exit=lambda: print("[process]", "process ended"), # $HighlightLine
+ )
+ await proc_with_custom_handler
+ # output: [process] process ended
+
+ await session.close()
+
+asyncio.run(main())
diff --git a/apps/docs/src/code/python/basics/process_stream_stderr.py b/apps/docs/src/code/python/basics/process_stream_stderr.py
index 83cac918f..f26efc0f9 100644
--- a/apps/docs/src/code/python/basics/process_stream_stderr.py
+++ b/apps/docs/src/code/python/basics/process_stream_stderr.py
@@ -4,19 +4,25 @@ from e2b import Session
E2B_API_KEY = getenv("E2B_API_KEY")
-def print_stderr(output):
- print(output.line)
-
async def main():
- session = await Session.create(id="Nodejs", api_key=E2B_API_KEY)
+ session = await Session.create(
+ id="Nodejs",
+ api_key=E2B_API_KEY,
+ on_stderr=lambda output: print("[session]", output.line), # $HighlightLine
+ )
+
+ # This command will fail and output to stderr because Golang isn't installed in the cloud playground
+ proc = await session.process.start("go version")
+ await proc
+ # output: [session] /bin/bash: line 1: go: command not found
- # This command will fail and output to stderr because Golang isn't installed in the cloud playground
- golang_version = await session.process.start(
- "go version",
- on_stderr=print_stderr, # $HighlightLine
- )
- await golang_version
+ proc_with_custom_handler = await session.process.start(
+ "go version",
+ on_stderr=lambda output: print("[process]", output.line), # $HighlightLine
+ )
+ await proc_with_custom_handler
+ # output: [process] /bin/bash: line 1: go: command not found
- await session.close()
+ await session.close()
asyncio.run(main())
diff --git a/apps/docs/src/code/python/basics/process_stream_stdout.py b/apps/docs/src/code/python/basics/process_stream_stdout.py
index 8bc99cf7b..0a4cea418 100644
--- a/apps/docs/src/code/python/basics/process_stream_stdout.py
+++ b/apps/docs/src/code/python/basics/process_stream_stdout.py
@@ -4,18 +4,24 @@ from e2b import Session
E2B_API_KEY = getenv("E2B_API_KEY")
-def print_stdout(output):
- print(output.line)
-
async def main():
- session = await Session.create(id="Nodejs", api_key=E2B_API_KEY)
+ session = await Session.create(
+ id="Nodejs",
+ api_key=E2B_API_KEY,
+ on_stdout=lambda output: print("session", output.line), # $HighlightLine
+ )
+
+ proc = await session.process.start('echo "Hello World!"')
+ await proc
+ # output: session Hello World!
- npm_init = await session.process.start(
- "npm init -y",
- on_stdout=print_stdout, # $HighlightLine
- )
- await npm_init
+ proc_with_custom_handler = await session.process.start(
+ 'echo "Hello World!"',
+ on_stdout=lambda output: print("process", output.line), # $HighlightLine
+ )
+ await proc_with_custom_handler
+ # output: process Hello World!
- await session.close()
+ await session.close()
asyncio.run(main())