Skip to content

Run commands inside a VM ​

cairn exec runs a command inside a running VM, and cairn console opens a shell there. Both are for debugging and one-off jobs, much like docker exec.

sh
cairn exec web -- nginx -v               # run one command
cairn exec web -- ls -la /etc
cairn exec -i web -- cat < file.txt      # send your input to the command
cairn exec -it web -- sh                 # an interactive terminal
cairn console web                        # a shell, picked for you

Everything after -- is the command and its arguments.

Examples ​

console
$ cairn exec web -- sh -c 'echo hi from $(hostname); cat /proc/1/comm'
hi from web
cairn-init
$ cairn exec web -- sh -c 'exit 7'; echo $?
7
console
$ cairn console web
cairn: connected to web (machine 5YE8WELG3J) as root. Type ~. to disconnect, ~? for help.
root@web:/#

Options ​

OptionMeaning
-iPass your input (stdin) to the command. Without it, the command's input is empty.
-tGive the command a terminal. -it together needs a real terminal on your side.
-u <user>Run as this user. By default, the same user as your app.
-w <dir>Run in this directory. By default, the same as your app.
-e KEY=VALUESet an environment variable. By default, the command gets your app's environment.

cairn console tries $SHELL (if it's an absolute path), then /bin/bash, /bin/sh and /bin/ash. --shell <path> picks one.

Things to know ​

  • There is no shell unless you ask for one. The command runs directly, so pipes, * and $VAR aren't expanded. Wrap the command in sh -c '...' when you need them.
  • Leaving an interactive session. With -it or console, type ~. at the start of a line to disconnect, and ~? for help.
  • Up to 8 sessions at a time per VM. Any processes a session started end with it.
  • The VM must be running. For a stopped app, run cairn start first.
  • The kernel needs cgroup.kill. cairn's default kernel has it. A custom kernel without it boots with exec turned off, and says so on the console.

Exit status ​

StatusMeaning
the command's own codeThe command ran and exited.
128 + NThe command was killed by signal N.
125cairn refused the request, and nothing ran.
126The command couldn't be run.
127The command wasn't found.
255The result is unknown (the connection was lost, for example).

Who can use it ​

exec can run commands as any user in the image, including root. Only you can use it on your own VMs, because it goes through a socket that only you can open. It doesn't give anyone access they didn't already have. See Security.