Running commands
Run commands and scripts on one or many servers, start long tasks, and save commands you use often.
Run a command
komando exec web1 -- uptime
10:42:01 up 34 days, 2:11, 1 user, load average: 0.12, 0.08, 0.05
Typed alone at your terminal, komando exec asks for the server (from your list), then the command.
Everything after -- is the command. It runs through the server's login shell, on all picked servers in parallel. komando's own flags go before --: exec web1 -- uptime --dry-run is refused, because --dry-run there would be part of the command and the command would really run. When the command on the server takes such a flag itself (apt-get upgrade --dry-run), give it as one string with -c.
The -- is required: komando exec web1 apt-get install -y nginx is a usage error (exit 2) and runs nothing, because komando would read -y as its own --yes. The same holds for every command that hands words to another program: job start, docker exec, docker compose, docker raw, cluster exec, cluster shell, cluster run and cache run.
komando exec @web -P 20 -- 'df -h / | tail -1' # up to 20 servers at once
komando exec tag:prod --sudo -- systemctl is-active nginx
komando exec db1 --read-only -- 'df -h /var/lib/postgresql' # SQL itself: komando db query
| Flag | What it does |
|---|---|
-c, --cmd 'COMMAND' | Give the command as one string instead of after --. |
-s, --sudo / --sudo-user NAME | Run as root, or as another user (uses the server's --sudo-ref password if set). |
-C, --dir PATH | Run in this folder on the server. |
-e, --env KEY=VALUE | Export a variable first (repeatable; a comma stays in the value). |
--timeout 10m | Time limit per server (default 5m). |
-P, --parallel 20 | How many servers at once (default 10). |
--fail-fast | Start no more servers after the first failure. |
--stream | Print output live, as it comes, a whole line at a time (in JSON: one event per line). |
--stdin-file FILE | Send a local file as the command's input (- = komando's own input). |
--pty | Give the command a terminal (some tools need one). |
--read-only | Say the command only looks (see below). |
Results and exit codes
Each server gets its own output, errors, exit code and run time. If the command fails on any server, komando exits 5. If komando cannot connect or log in, it exits 4, except when the server does not answer within --connect-timeout (CONNECT_TIMEOUT): that exits 7, like other timeouts.
With -o text, one server prints like ssh: its output, its errors, and a closing Error: server web1 failed (exit 3) when it fails. Several servers get a block each:
=== web1 [ok, exit 0]
active
=== web2 [failed: exit 3]
[stderr] inactive
Error: 1 of 2 failed: web2 (exit 3)
With --fail-fast, servers that never started are reported as skipped, not failed. With --parallel 1, servers run one by one, in name order. Output longer than --max-output (256 KiB per stream) is cut, and komando says so.
When the connection drops
Sometimes the link to a server breaks while a command is running: the VPN drops, the Wi-Fi changes, a firewall cuts the connection. komando then cannot know how the command ended. That server's result says so:
- error code
CONNECTION_LOST, exit code 4 (other servers keep their own results; the worst exit code wins, as usual); - the output that arrived before the break, with
exit_code-1 (unknown); - in text mode:
=== web2 [failed: connection lost, outcome unknown].
The command may still be running on the server, or it may already have finished. Do not run it again yet (an upgrade or a migration twice can hurt). First look, with a read-only command: its log file, ps, or the change it should have made:
komando exec web2 --read-only -- 'tail -20 /var/log/unattended-upgrades/unattended-upgrades.log'
komando exec web2 --read-only -- 'ps -ef | grep -v grep | grep apt'
If the server stops answering (a VPN that went away without a word), komando notices within about a minute instead of waiting for --timeout.
This is different from:
| What happened | Error code | Exit |
|---|---|---|
| could not connect at all (the command never started) | CONN_UNREACHABLE, CONNECTION_REFUSED, AUTH_FAILED, ... | 4 |
| the link broke before the command started (it did not run) | CONNECTION_LOST, message says "it did not run" | 4 |
| the link broke while the command ran (outcome unknown) | CONNECTION_LOST | 4 |
| the command ran and failed | COMMAND_FAILED (per server: its exit_code) | 5 |
the command ran longer than --timeout (komando stopped it) | TIMEOUT | 7 |
For long tasks, or any task over a shaky link, use a job instead: it keeps running on the server, keeps its output and exit code there, and job wait connects again by itself after a drop.
Protected servers and --read-only
On a protected server, a command asks first (at your terminal komando asks you; an AI agent or a script adds --yes after a person agrees; see Safety), unless you add --read-only to say it only looks.
--read-only is a strict allowlist, on every server: the command runs only when komando knows every part of it only reads. Anything else is refused with READ_ONLY_VIOLATION; drop --read-only and confirm the change instead. What passes:
- Plain read tools:
cat,tail,head,ls,stat,df,du,free,uptime,ps,grep,wc,id,jqand the like, also in a pipe, aforloop or aftersh -c. - Tools whose options decide, with the options komando knows read:
findwithout-delete/-exec,sed -n,sort,awkthat only prints,journalctl,dmesg,ss,tar -t,openssl x509. A long option is written in full (--quiet, not--qui): a tool reads a shortened name as any option that starts with it,sed --inas--in-place. - Commands with subcommands, in the forms that read:
systemctl status,is-active,list-units,show;docker ps,logs,inspect,stats;kubectl get,describe,logs,top;ip addr,ip route;iptables -L/-S;apt list,dpkg -l,rpm -q;nginx -t,sshd -T. A subcommand komando does not know is refused, not waved through. sudo -n,sudo -u USER,timeout 10,nice,env NAME=VALUE,timeandxargs(with a plain read tool) in front of such a command, andset -e,-uand-o pipefail.
What does not pass, besides clear changes (rm, tee, > file, sed -i, systemctl restart, apt install): a program komando does not know (your own tool, curl, wget, nc), git (a repository's own config can run a program on git status or git diff), an option or subcommand it has not heard of, kubectl --kubeconfig and other client settings, sudo -E, unquoted braces ({a,b}), ${x@P} and other expansions bash evaluates, let, printf -v, test -v, set -x, psql and mysql (nothing holds them to a read-only transaction; use komando db query, which is), and setting a variable with an upper-case name (PATH, PS4, TAR_OPTIONS, KUBECONFIG ...: programs read such names as settings or code) by NAME=, export, read, for or -e. Use a name with a lower-case letter (-e dir=/var/log -- 'du -sh $dir'); LANG, LC_*, TZ, TERM and COLUMNS are fine. A command that uses an -e value has no double quotes: in "$dir" the value is one word komando cannot check. A value with *, ?, [, {, ~ or a backslash is refused: bash would expand it into names komando cannot see. kubectl config view --raw is refused too: it prints the kubeconfig's credentials. For a server's state, komando's own reads need no shell command at all: komando facts, logs, svc status, fs read, docker ps, cert check. docker exec and cluster exec check their --read-only the same way.
A command komando cannot follow is refused the same way: eval, source, a command name in a variable ($X file), a shell or python reading a script it cannot see (... | sh, python3 -c). Text after sh -c or bash -lc is checked like the command. With --stdin-file, the input is checked too, so --read-only --stdin-file check.sh -- 'bash -s' works when the script only looks. exec --script --read-only needs a shell as the interpreter (sh, bash -eu). Script arguments (--arg, or words after bash -s --) are refused under --read-only: put the values in the script. An argument the check cannot see (find /data $X) is refused for commands whose arguments decide what they do (find, sed, sort, kubectl ...); -e values are filled in first. Other input (a CSV for wc) is data and is not read as commands.
komando blocks destructive commands (rm -rf /, mkfs, reboot, DROP DATABASE, ...) everywhere unless a person allows them. See Safety.
Pass a password to a command
A password for the remote command can come from the vault, so it is never typed or shown: -e DB_PASS=secret:prod-db. komando fills in the value on the way to the server. It goes to the command on its input, not on its command line, so other users of the server do not see it in ps and it is not written to sudo's log. Values that are not from the vault are on the command line as usual.
Times, sizes and KEY=VALUE flags
Every flag that takes a time reads it the same way: a number and a unit, such as 30s, 10m, 2h, 1h30m or 7d. A number without a unit is refused (--older-than 30 could mean seconds or days), except 0.
Every size flag (--max-bytes, --max-output) takes bytes (1048576) or a size with a unit: 256KiB, 10MB, 1GB. KB and KiB both mean 1024 bytes, MB and MiB both 1024 KiB.
A KEY=VALUE flag (--env, --label, --option) takes one pair each time; repeat it for more: --label team=ops --label env=prod. A value keeps its commas (--option hosts=a,b), and a second KEY= after a comma is refused, so --option a=on,b=x is never read as one value.
How many: --limit
Every list command takes -n/--limit N, the most items to show (in cluster commands --limit only, since -n is the namespace there). Log readers show the last N lines. A command that runs on several servers counts per server. Without --limit a list shows what it always did: everything, or the default its help names (fs list 1000, logs 200). --limit 0 shows all where the help says so. When a list stops at its limit, the JSON has meta.truncated: true and -o text prints on stderr Note: showing the first N; --limit 0 shows all (some commands put their own cut note in their output instead).
Other names
Help shows one name per command. Plurals and the words people often type (servers, databases, ls for list) find the same command, and komando schema NAME says which command a name runs.
Run a local script
komando exec TARGET --script FILE sends a script file from this computer to the servers and runs it. Nothing is left on the server's disk.
komando exec @web --script ./deploy.sh --arg v1.2.3 --sudo
komando exec db1 --script ./backup.py --interpreter python3
The interpreter comes from the script's #! line, or from --interpreter (otherwise sh). --arg values reach the script as $1, sys.argv[1], $ARGV[0] and so on, for sh, bash, python, perl, ruby and node alike. The script is sent as the interpreter's input, so --stdin-file and --pty do not go with --script. Through env (#!/usr/bin/env -S bash -e), the program after env's own options is the interpreter, with its arguments. A script that holds a value komando hid ([hidden]) is refused (HIDDEN_VALUE), as is such a file given to exec --stdin-file.
Long tasks (jobs)
Anything slow (backups, upgrades, builds) should run as a job. A job keeps running on the server even if you disconnect, and job start returns at once with a job ID per server.
komando job start web1 -- 'tar czf /backup/www.tgz /var/www'
ID HOST PID STATE
j1108-193010-9f8e7d6c web1 48213 started
Typed alone, komando job start asks for the server, then the command.
komando job start @web --sudo -- 'apt-get update && apt-get -y upgrade'
komando job start @web --script ./migrate.sh --arg v2 # a script from this computer
komando job start db1 --sudo-user postgres --stdin-file ./fix.sql -- 'psql app' # as another user, with input
komando job start web1 --timeout 2h -- ./reindex.sh # stopped if it runs longer than 2 hours
komando job status JOB # running, exited or lost; exit code; last lines
komando job logs JOB # output so far
komando job wait JOB --timeout 30m # wait until it ends
komando job wait JOB1 JOB2 # wait for several (one per server)
komando job kill JOB # stop it (TERM; --signal KILL for a hard stop)
komando job list # every job; --refresh asks the servers
komando job rm JOB # forget it and delete its remote logs
Good to know:
job logs --offset Nreads only what is new: pass back thenext_offsetfrom the previous call.-n/--limit Ngives the last lines instead.job waitwaits up to--timeout(default 10m). When the time is up, the job keeps running and komando exits 7 (WAIT_TIMEOUT). Otherwise the exit code follows the job's. If the connection drops while it waits, it connects again and keeps waiting.job waitwith several IDs waits for all of them and gives one result per job (data.jobs) and a summary; the exit code is the worst one.job start --script FILEruns a script from this computer as the job, with--argand--interpreteras inexec --script. Its copy waits in the job's private folder and is removed the moment it is sent to the interpreter.job start --stdin-file FILEgives the command FILE as its input (-for komando's own stdin), and--sudo-user USERruns it as USER withsudo -u, as inexec. The job's--envvalues and its input go to the command through a pipe, so a command run as another user gets them too.job start --timeout 2hstops the job after 2 hours, asjob killdoes;job statusthen sayskilledwithtimed_out: true, andjob waitnames the timeout. Without--timeouta job has no time limit. (job starthas no--pty: a job runs without a terminal.)- When
exectimes out or loses its connection, its hint is thekomando job start ...line that runs the same thing as a job. job start --sudouses the server's--sudo-refpassword when it has one. The password is sent as input (never on a command line) and waits in a private file only the job's user can read. The job first asks sudo whether it needs a password: when it does not, the file is deleted unread; when it does, sudo reads it before the command starts. Either way the command can never read it.job killon a job that already ended does nothing. A job started with--sudoneedsjob kill --sudo.job rmrefuses a running job: kill it first, or add--kill.--keep-remotekeeps the log folder on the server.job list --target @webshows only jobs on those servers.
Recipes (ready-made and saved commands)
A recipe is a saved command. komando comes with ready-made ones (health, disk, memory and more; see Health), and you can save your own next to them.
komando recipe list # built-in and saved
komando recipe list --category saved # only your own
komando recipe add restart-app 'systemctl restart {{app}}' --needs-sudo
komando recipe add disk-of 'df -h {{path=/}}' --read-only --desc 'Disk use of a path'
komando recipe run restart-app @web --var app=shop
komando recipe run disk-of web1 --var path=/srv
komando recipe rm disk-of
{{app}}is a blank you fill in with--var app=....{{path=/}}has a default.- komando treats a recipe as a change, so a protected server asks first, unless you saved it with
--read-onlybecause it only looks. A read-only recipe is checked likeexec --read-onlywhen you save it. If, with its blanks filled in, it no longer passes that check, it runs as a change. - A
--varvalue may not start with-, because a program would read it as an option (OPTION_LIKE_VAR). Write a path like that as./-name. A number or a relative time (-5,-2h) is fine. - A built-in name (
disk,health, ...) cannot be reused. Replace one of your own with--overwrite. - A
--varthe recipe has no blank for is refused (UNKNOWN_VAR), so a typo never runs the recipe with its default. recipe run --no-sudoskips sudo even when the recipe asks for it.
At your terminal: komando asks for what is missing
There is one way to use komando: komando NOUN VERB [TARGET] [FLAGS]. At your own terminal, leave something out and komando asks for it, one line at a time, in the normal screen (your scrollback keeps it):
$ komando logs
1 web1 ubuntu@10.0.0.5
2 web2 ubuntu@10.0.0.6
TARGET: 1
$ komando logs web1
...
Next time: komando logs web1
- It asks only what the command needs: missing arguments, required flags, and the few values a command is useless without (the SQL of
db query, the command ofexec). Labels are the real names:CONN (database):,TARGET:,--old:.?explains the question; Enter takes the default in brackets. - Lists come from what komando knows: a number picks a row, part of a name narrows the list, and a selector (
@web,tag:db) or a new name is taken as typed. With one item that is not protected komando uses it and says so (Using prod-db (the only one of your databases)); a protected one is never picked for you. With none it says how to add one. - It then shows the whole line (
$ komando ...). A read runs at once. A change asksRun it? [y/N, d = --dry-run first](Enter is No;dshows what it would do and asks again). One that deletes for good asks you to type the name of what goes (or how many). A protected target asks its usualweb1 (protected): this command changes it. Continue? [y/N]instead. komando host add(orconn add,vpn add,db add,cache add,queue add,storage add,cluster add,registry add) typed alone asks that kind's own questions: which kind, a name, the address or the parts of the URL, the user, the port, how you log in, and whether it is production. A password is never asked here: the command asks for it itself, hidden (--ask-password). A password pasted with the address (root:secret@10.0.0.5) is taken out before it is drawn. After the add,komando testshows that it answers; if it does not, komando says the commands to change, test again or remove it.- Ctrl-C at a question stops it and nothing runs. komando never adds
--yes,--allow-dangerous,--revealor--writefor you. - After a command you ran ends well, komando may say what usually comes next, on one line on stderr with at most three commands (
Next: komando db info prod-db). Every add, edit, rename and rm has one. Nothing "next" is printed on stdout; scripts and agents readdata.next.komando host show web1(conn show,vpn show,group list GROUP,key show,job status,secret show,config get) ends with the commands for that item.
komando alone shows the commands by group, START first. A command typed in full runs as it always did. Nothing is ever asked of an AI agent, a script, a pipe, a CI job (CI set), a dumb terminal (TERM=dumb) or JSON output: they get the usual USAGE error that names what is missing.
Interactive shell (for people)
komando shell web1 # log in, with a status line first
komando shell web1 --dry-run # shows the server, address, user and --via path; connects nothing
shell logs you in to the server, through its --via path (VPN and bastions) when it has one. Inside a shell you can type anything, so on a protected server komando asks [y/N] first, as db shell does on a protected conn. A shell needs a person at a terminal: agents and scripts get NEEDS_TERMINAL and use exec or job instead.
Where am I? (status lines)
When you type a command yourself, komando prints one line saying where you are working, before it connects. It does this for the commands you stay inside: shell, db shell, cluster shell, cluster forward, logs --follow and cluster logs -f.
komando: shell on web1 (deploy@10.0.1.5) via vpn office → bastion
komando: shell on db1 (root@10.0.0.5) · PROTECTED
komando: psql on prod-db (read-only) via bastion
komando: forwarding 127.0.0.1:8080 → svc/api:80 on prod-k8s (Ctrl-C stops)
komando: following logs on web1, web2 (Ctrl-C stops)
viais the way there: the VPN first, then the bastions.- A protected server or database says
PROTECTED, in red unlessNO_COLORis set. - On a terminal that cannot show them (
TERM=dumb, or a locale such asLANG=Cthat is not UTF-8), komando writes the marks in its own lines (errors, hints, warnings) in ASCII:+for ✓,xfor ✗,*for ●,->for →,-for ·. A result (a file, a row, a log line) is always shown as it is. - When a
shellordb shellends, one more line says how:komando: left web1 (exit 0, 12m)orkomando: connection to web1 lost after 3m. - When a command connects a VPN by itself, komando says that the VPN stays up and how to stop it.
These lines go to stderr, and only to a person at a terminal. Agents, scripts and pipes never get them, and the JSON output does not change.
See what is still running
Some things keep running after the command that started them: a VPN (from vpn up, or connected by a command), a server login kept by komando login, a tunnel, or a job on a server. komando status lists them all:
komando status
KIND NAME DETAIL SINCE
vpn office used 3m ago, closes in 26m if unused 42m
login web1 kept for 7h more (`komando logout web1` ends it) 1h
aws dev,ops SSO login, kept for 3h more (`komando logout aws --profile dev` ends it) 1h
tunnel t1108-195255-ab12cd34 127.0.0.1:5433 → prod-db 2h
job j1108-193010-9f8e7d6c web1: tar czf /backup/www.tgz /var/www 15m
jobs run on their servers: `komando job list --refresh` shows which still run
With nothing running it says nothing running: no VPN sessions, server logins, tunnels or jobs.
- It contacts nothing, so it is quick and works offline.
- A job stays on the list until
komando job rm. Whether it still runs is known only on its server, whichkomando job list --refreshasks. - Agents get the same as JSON (
vpns,logins,tunnels,jobs). - An idle VPN closes after
vpn_idle(see Settings).
Show it in your shell prompt
komando status --short prints a short note such as vpn:2 tun:1 (how many VPNs and tunnels are up), or nothing when nothing is running, so your prompt can show it all the time. Add it yourself; komando never changes your shell files.
zsh (also with oh-my-zsh), in ~/.zshrc:
setopt PROMPT_SUBST
RPROMPT='$(komando status --short)'
bash, in ~/.bashrc:
PS1='$(p=$(komando status --short); [ -n "$p" ] && echo "[$p] ")'"$PS1"
starship, in ~/.config/starship.toml:
[custom.komando]
command = "komando status --short"
when = true
It reads only the process numbers komando keeps beside its store (no names, no key, no network, nothing written), prints no JSON and never fails, so it is safe to run on every prompt. It is a quick hint: a VPN that crashed a moment ago can still show until the next komando command. komando status is the full check.
Limits
- The
--before a command's words is required for every command that hands words to another program; komando's own flags go before it. - Output longer than
--max-output(256 KiB per stream) is cut. --timeoutis per server (default 5m); a job is the way past it.--read-onlyruns only commands komando knows only read, on every target; script arguments (--arg, words afterbash -s --) are refused under it.exec --scriptcannot be combined with--stdin-fileor--pty.- A recipe
--varvalue may not start with-(except a number or a time back from now). - A built-in recipe name cannot be reused for your own.
shelland the questions for missing arguments need a person at a terminal: nothing is asked of an AI agent, a script, a pipe, a CI job (CIset), a dumb terminal (TERM=dumb) or JSON output.komando statuslists jobs from komando's own records; onlyjob list --refreshasks the servers.
Troubleshooting
| Error code | What it means | What to do |
|---|---|---|
USAGE | an argument is missing, or the command's words were not after -- (exit 2) | read the example in the error; put the command after -- |
COMMAND_FAILED | the command ran and failed on a server (exit 5) | read that server's output and exit_code |
CONNECTION_LOST | the link broke; the command may still run, or may have finished (exit 4) | look first with a read-only command; use a job over a shaky link |
CONNECT_TIMEOUT | the server did not answer within --connect-timeout (exit 7) | check the address and the --via path |
TIMEOUT | the command ran longer than --timeout; komando stopped it (exit 7) | raise --timeout, or run it as a job |
READ_ONLY_VIOLATION | --read-only, but the command changes something or komando cannot follow it (exit 6) | drop --read-only; a protected server then asks first |
HIDDEN_VALUE | a script or --stdin-file holds a value komando hid ([hidden]) | put secret:NAME in -e instead |
WAIT_TIMEOUT | job wait ran out of time; the job still runs (exit 7) | komando job wait JOB again, or komando job logs JOB --limit 50 |
JOB_RUNNING | job rm on a running job | komando job kill JOB first, or add --kill |
JOB_NOT_FOUND | no job with that ID | see them with komando job list |
OPTION_LIKE_VAR | a recipe --var value starts with - | write a path like that as ./-name |
UNKNOWN_VAR | the recipe has no blank of that name | check its blanks with komando recipe list |
NEEDS_TERMINAL | shell without a person at a terminal | use komando exec or komando job start |
Source: docs/features/commands.md in sgnd/komando at commit b622784.