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