Shell
Work in a sandbox on a terminal, or give a program a terminal.
Last updated
Description and operation
General
A shell is a sandbox whose main process is a shell program, run on a terminal with its input open. The program sees a real terminal: colours, prompts, full-screen programs and Ctrl-C work.
You create the sandbox with tty and stdin, and a shell program as cmd. Then you attach to it. The attach connects your terminal to the process.
The program starts when you attach, or after 60 seconds without an attach. The attach stays open while the program is idle.
The CLI passes the terminal type of your own terminal (TERM) to the sandbox. The sandbox uses xterm-256color when your terminal has none.
| Item | Value |
|---|---|
cmd | The shell program, for example bash, or any other program. |
tty | True. The process runs on a terminal. |
stdin | True. The input stays open so that you can attach. |
| Detach | Close the attach. The process keeps running. |
| End | The sandbox ends when the shell exits, when you end it, or at its time limit. |
A shell has no time limit of its own. It has the timeout_s of the sandbox. The CLI needs a terminal on its standard input and output.
Tasks
Open a shell in a sandbox
Reason for the task: Work in a sandbox on a terminal, with prompts, vim, top and Ctrl-C, or give a program a terminal.
Job set-up
- Access: you.
- References:
POST /v1/sandboxes,GET /v1/sandboxes/{id}/attach.
The CLI needs a terminal on its standard input and output. For a command without a terminal, see Commands.
In the CLI, kloudlite sandboxes run -it -- bash does steps 1 and 2 together: it creates the sandbox and connects to it. While the sandbox starts, the CLI shows a status line on the error output.
Procedure
Create the sandbox with a shell as the command, a terminal and an open input.
sb = kl.sandboxes.create(cmd=["bash"], tty=True, stdin=True, timeout_s=1800)const sb = await kl.sandboxes.create({ cmd: ["bash"], tty: true, stdin: true, timeoutS: 1800 });sb, err := kl.Sandboxes.Create(kloudlite.CreateSandbox{Cmd: []string{"bash"}, Tty: true, Stdin: true, Timeout: 1800 * time.Second})if err != nil { log.Fatal(err)}sb = kl.sandboxes.create(cmd: ["bash"], tty: true, stdin: true, timeout_s: 1800)var sb = kl.sandboxes().create(new Kloudlite.CreateSandbox().cmd(List.of("bash")).tty(true).stdin(true).timeout(Duration.ofSeconds(1800)));kloudlite sandboxes create -it --timeout-s 1800 -- bashConnect to the shell. The call waits until the sandbox is
running.sh = sb.attach()const sh = await sb.attach();sh, err := sb.Attach(80, 24)if err != nil { log.Fatal(err)}defer sh.Close()sh = sb.attachvar sh = sb.attach();kloudlite sandboxes attach sbx-3c4d5e6f7a8b9c0dType a command and read the output. End the input with
exit.sh.write("echo $((6 * 7)); exit\n")while (chunk := sh.read()) is not None: print(chunk.decode(), end="")sh.write("echo $((6 * 7)); exit\n");for (let c = await sh.read(); c !== null; c = await sh.read()) process.stdout.write(c);sh.Write([]byte("echo $((6 * 7)); exit\n"))if _, err := io.Copy(os.Stdout, sh); err != nil { log.Fatal(err)}sh.write("echo $((6 * 7)); exit\n")while (chunk = sh.read) print chunkendsh.write("echo $((6 * 7)); exit\n");for (byte[] c = sh.read(); c != null; c = sh.read()) System.out.write(c, 0, c.length);System.out.flush();echo $((6 * 7)); exitWait for the shell to exit. The call returns the exit code.
print(sh.wait())sh.close()console.log(await sh.wait());sh.close();code, err := sh.Wait()if err != nil { log.Fatal(err)}fmt.Println(code)puts sh.waitsh.closeSystem.out.println(sh.waitFor());sh.close();echo $?
Close-out
- Make sure that the output shows
42and the exit code is0.
Call resize with the new columns and rows when your window changes. The CLI does this by itself.
To leave a shell running, close the attach and do not type exit. Attach again with the id of the sandbox. To run a program other than a shell, give it as cmd.
Troubleshooting
| Symptom | Possible cause | Corrective action |
|---|---|---|
409 from the attach route, for stdin | The sandbox was created without stdin. | Create a new sandbox with stdin set. |
409 with has terminated | The shell exited, or the sandbox ended. | Create a new sandbox. Read the output with logs. |
426 from the attach route | The request is not a WebSocket upgrade. | Use the SDK or the CLI, or open the route as a WebSocket. |
404 with not found | The sandbox does not exist, or is not yours. | Check the id in the run records. |
| The shell ends when you leave | The shell program exited. | Close the attach. Do not type exit. |