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 youEverything 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 $?
7console
$ cairn console web
cairn: connected to web (machine 5YE8WELG3J) as root. Type ~. to disconnect, ~? for help.
root@web:/#Options
| Option | Meaning |
|---|---|
-i | Pass your input (stdin) to the command. Without it, the command's input is empty. |
-t | Give 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=VALUE | Set 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$VARaren't expanded. Wrap the command insh -c '...'when you need them. - Leaving an interactive session. With
-itorconsole, 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 startfirst. - 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
| Status | Meaning |
|---|---|
| the command's own code | The command ran and exited. |
128 + N | The command was killed by signal N. |
125 | cairn refused the request, and nothing ran. |
126 | The command couldn't be run. |
127 | The command wasn't found. |
255 | The 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.