Skip to content
DocsAll docs
Console

Shell

Work in a sandbox on a terminal, or give a program a terminal.

Last updated

On this page

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.

ItemValue
cmdThe shell program, for example bash, or any other program.
ttyTrue. The process runs on a terminal.
stdinTrue. The input stays open so that you can attach.
DetachClose the attach. The process keeps running.
EndThe 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

NOTE

The CLI needs a terminal on its standard input and output. For a command without a terminal, see Commands.

NOTE

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

  1. Create the sandbox with a shell as the command, a terminal and an open input.

    Python
    sb = kl.sandboxes.create(cmd=["bash"], tty=True, stdin=True, timeout_s=1800)
    TypeScript
    const sb = await kl.sandboxes.create({ cmd: ["bash"], tty: true, stdin: true, timeoutS: 1800 });
    Go
    sb, err := kl.Sandboxes.Create(kloudlite.CreateSandbox{Cmd: []string{"bash"}, Tty: true, Stdin: true, Timeout: 1800 * time.Second})if err != nil {    log.Fatal(err)}
    Ruby
    sb = kl.sandboxes.create(cmd: ["bash"], tty: true, stdin: true, timeout_s: 1800)
    Java
    var sb = kl.sandboxes().create(new Kloudlite.CreateSandbox().cmd(List.of("bash")).tty(true).stdin(true).timeout(Duration.ofSeconds(1800)));
    CLI
    kloudlite sandboxes create -it --timeout-s 1800 -- bash
  2. Connect to the shell. The call waits until the sandbox is running.

    Python
    sh = sb.attach()
    TypeScript
    const sh = await sb.attach();
    Go
    sh, err := sb.Attach(80, 24)if err != nil {    log.Fatal(err)}defer sh.Close()
    Ruby
    sh = sb.attach
    Java
    var sh = sb.attach();
    CLI
    kloudlite sandboxes attach sbx-3c4d5e6f7a8b9c0d
  3. Type a command and read the output. End the input with exit.

    Python
    sh.write("echo $((6 * 7)); exit\n")while (chunk := sh.read()) is not None:    print(chunk.decode(), end="")
    TypeScript
    sh.write("echo $((6 * 7)); exit\n");for (let c = await sh.read(); c !== null; c = await sh.read()) process.stdout.write(c);
    Go
    sh.Write([]byte("echo $((6 * 7)); exit\n"))if _, err := io.Copy(os.Stdout, sh); err != nil {    log.Fatal(err)}
    Ruby
    sh.write("echo $((6 * 7)); exit\n")while (chunk = sh.read)  print chunkend
    Java
    sh.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();
    CLI
    echo $((6 * 7)); exit
  4. Wait for the shell to exit. The call returns the exit code.

    Python
    print(sh.wait())sh.close()
    TypeScript
    console.log(await sh.wait());sh.close();
    Go
    code, err := sh.Wait()if err != nil {    log.Fatal(err)}fmt.Println(code)
    Ruby
    puts sh.waitsh.close
    Java
    System.out.println(sh.waitFor());sh.close();
    CLI
    echo $?

Close-out

  1. Make sure that the output shows 42 and the exit code is 0.

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

SymptomPossible causeCorrective action
409 from the attach route, for stdinThe sandbox was created without stdin.Create a new sandbox with stdin set.
409 with has terminatedThe shell exited, or the sandbox ended.Create a new sandbox. Read the output with logs.
426 from the attach routeThe request is not a WebSocket upgrade.Use the SDK or the CLI, or open the route as a WebSocket.
404 with not foundThe sandbox does not exist, or is not yours.Check the id in the run records.
The shell ends when you leaveThe shell program exited.Close the attach. Do not type exit.