1+ package io .openkruise .agents .client .e2b .example ;
2+
3+ import io .grpc .ManagedChannel ;
4+ import io .openkruise .agents .client .e2b .config .ConnectionConfig ;
5+ import io .openkruise .agents .client .e2b .sandbox .commands .CommandResult ;
6+ import io .openkruise .agents .client .e2b .sandbox .commands .CommandHandle ;
7+ import io .openkruise .agents .client .e2b .sandbox .commands .Commands ;
8+ import io .openkruise .agents .client .e2b .sandbox .commands .Commands .RunOptions ;
9+ import io .openkruise .agents .client .e2b .sandbox .commands .Commands .ProcessInfo ;
10+
11+ import java .util .List ;
12+ import java .util .HashMap ;
13+ import java .util .Map ;
14+ import java .util .function .Consumer ;
15+
16+ public class CommandsExample {
17+ private static final String ACCESS_TOKEN = "sandbox-token" ;
18+ private static final String API_KEY = "key" ;
19+ private static final String SANDBOX_ID = "sandbox-id" ;
20+ private static final String SANDBOX_DOMAIN = "your.domain.com" ;
21+
22+ public void CommandsExample () throws Exception {
23+ ConnectionConfig config = new ConnectionConfig .Builder ()
24+ .apiKey (API_KEY )
25+ .debug (true )
26+ .requestTimeoutMs (30000 )
27+ .build ();
28+
29+ ManagedChannel channel = config .createChannel (SANDBOX_ID , ACCESS_TOKEN , SANDBOX_DOMAIN );
30+
31+ try {
32+ Commands commands = new Commands (channel , config );
33+
34+ System .out .println ("=== Example 1: Execute Simple Command ===" );
35+ CommandResult pwd = commands .run ("pwd" , new RunOptions ().timeoutMs (10000 ));
36+ System .out .println ("pwd command result: " + pwd );
37+
38+ System .out .println ("\n === Example 2: Execute Complex Command ===" );
39+ CommandResult ls = commands .run ("ls -la /tmp" , new RunOptions ().timeoutMs (10000 ));
40+ System .out .println ("ls -la /tmp result: " );
41+ System .out .println (ls .getStdout ());
42+ if (!ls .getStderr ().isEmpty ()) {
43+ System .out .println ("Error output: " + ls .getStderr ());
44+ }
45+
46+ System .out .println ("\n === Example 3: Execute Command With Environment Variables ===" );
47+ Map <String , String > envs = new HashMap <>();
48+ envs .put ("MY_VAR" , "hello_world" );
49+ RunOptions optionsWithEnv = new RunOptions ()
50+ .envs (envs )
51+ .timeoutMs (10000 );
52+ CommandResult envTest = commands .run ("echo $MY_VAR" , optionsWithEnv );
53+ System .out .println ("Environment variable test result: " + envTest .getStdout ().trim ());
54+
55+ System .out .println ("\n === Example 4: Execute Background Command And Wait For Completion ===" );
56+ CommandHandle bgHandle = commands .runBackground ("sleep 3 && echo 'Background task completed'" ,
57+ new RunOptions ().timeoutMs (10000 ));
58+ System .out .println ("Background command PID: " + bgHandle .getPid ());
59+ CommandResult bgResult = bgHandle .waitForCompletion ();
60+ System .out .println ("Background command result: " + bgResult .getStdout ().trim ());
61+
62+ System .out .println ("\n === Example 5: Execute Real-time Output Command ===" );
63+ // Use callback function to handle output in real-time
64+ Consumer <String > stdoutHandler = output -> System .out .print ("STDOUT: " + output );
65+ Consumer <String > stderrHandler = output -> System .out .print ("STDERR: " + output );
66+ RunOptions realtimeOptions = new RunOptions ()
67+ .onStdout (stdoutHandler )
68+ .onStderr (stderrHandler )
69+ .timeoutMs (15000 );
70+ CommandResult realtimeResult = commands .run ("for i in {1..5}; do echo \" Count: $i\" ; sleep 1; done" ,
71+ realtimeOptions );
72+ System .out .println ("\n Real-time output command completed" );
73+
74+ System .out .println ("\n === Example 6: Execute Command Requiring Working Directory ===" );
75+ RunOptions cwdOptions = new RunOptions ()
76+ .cwd ("/tmp" )
77+ .timeoutMs (10000 );
78+ CommandResult cwdResult = commands .run ("pwd && ls -la" , cwdOptions );
79+ System .out .println ("Working directory command result:\n " + cwdResult .getStdout ());
80+
81+ System .out .println ("\n === Example 7: Start Long-running Process And List All Processes ===" );
82+ CommandHandle longRunningHandle = commands .runBackground ("sleep 20 && echo 'Long-running task completed'" ,
83+ new RunOptions ().timeoutMs (25000 ));
84+ System .out .println ("Start long-running process, PID: " + longRunningHandle .getPid ());
85+
86+ // Wait a moment for the process to start
87+ Thread .sleep (1000 );
88+
89+ // List all processes
90+ System .out .println ("\n === Current Running Process List ===" );
91+ List <ProcessInfo > processes = commands .list ();
92+ for (ProcessInfo process : processes ) {
93+ System .out .printf ("PID: %d, CMD: %s, CWD: %s%n" ,
94+ process .getPid (), process .getCmd (), process .getCwd ());
95+ }
96+
97+ System .out .println ("\n === Example 8: Send Standard Input To Running Process ===" );
98+ // Start an interactive process
99+ CommandHandle interactiveHandle = commands .runBackground (
100+ "read -p 'Please enter content: ' input && echo 'You entered: $input'" ,
101+ new RunOptions ().stdin (true ).timeoutMs (10000 ));
102+ System .out .println ("Interactive command PID: " + interactiveHandle .getPid ());
103+
104+ // Wait a moment for the process to be ready to receive input
105+ Thread .sleep (1000 );
106+
107+ // Send standard input
108+ commands .sendStdin (interactiveHandle .getPid (), "Hello from stdin\n " );
109+
110+ // Wait for completion
111+ CommandResult interactiveResult = interactiveHandle .waitForCompletion ();
112+ System .out .println ("Interactive command result: " + interactiveResult .getStdout ());
113+
114+ System .out .println ("\n === Example 9: Terminate Running Process ===" );
115+ // Start another long-running process
116+ CommandHandle toKillHandle = commands .runBackground ("sleep 30 && echo 'Should not see this message'" ,
117+ new RunOptions ().timeoutMs (30000 ));
118+ System .out .println ("Start process to terminate, PID: " + toKillHandle .getPid ());
119+
120+ Thread .sleep (1000 );
121+
122+ // Kill the process
123+ boolean killed = commands .kill (toKillHandle .getPid ());
124+ System .out .println ("Process termination result: " + killed );
125+
126+ System .out .println ("\n === Example 10: Check Process List To Confirm Cleanup ===" );
127+ List <ProcessInfo > finalProcesses = commands .list ();
128+ System .out .println ("Final process count: " + finalProcesses .size ());
129+ for (ProcessInfo process : finalProcesses ) {
130+ System .out .printf ("PID: %d, CMD: %s%n" , process .getPid (), process .getCmd ());
131+ }
132+
133+ System .out .println ("\n ========== All Command Examples Executed ==========" );
134+
135+ } finally {
136+ channel .shutdown ();
137+ }
138+ }
139+ }
0 commit comments