Remote Execution
Execute commands on one or many remote hosts with fan-out execution.
Nefia's exec command lets you run shell commands on one or more remote hosts simultaneously. Combined with target selector expressions, concurrency control, batching, reruns, and offline queueing, it provides a safe and predictable way to operate on your entire fleet.
Basic Usage
The command to execute must follow a -- separator:
nefia exec [flags] -- <command>Run a command on a single host using the --host (or -H) shorthand:
nefia exec --host pc-office -- hostname[pc-office] pc-office.local
To run on all connected hosts, use --target all:
nefia exec --target all -- uptime[web-01] up 14 days, 3:22 [web-02] up 14 days, 3:20 [db-01] up 42 days, 7:15
3/3 hosts succeeded
Target Selection
Use --target (or -t) to select which hosts to run on. The --host (-H) flag is a shorthand for targeting a single host by name.
| Selector | Syntax | Example |
|---|---|---|
| Single host (shorthand) | --host <name> or -H <name> | --host pc-office |
| Host by ID | --target host:<id> | --target host:abc123 |
| Group | --target group:<name> | --target group:dev-team |
| Tag | --target tag:<key>=<value> | --target tag:env=production |
| All hosts | --target all | --target all |
Selector Expressions
--target is evaluated as a single selector expression. Inside that expression, you can combine unions, exclusions, intersections, regexes, and host lists:
,for union!for exclusion&for intersection~regexfor host ID regex matching@path/to/fileto read host IDs from a file-to read host IDs from stdin
# Union
nefia exec --target 'group:webservers,group:workers' -- systemctl status app
# Exclude one host from a group
nefia exec --target 'group:webservers,!host:web-03' -- uptime
# Regex
nefia exec --target '~^prod-' -- hostnameSession-Scoped Execution
Use --session (or -s) to execute a command within an existing session context:
nefia exec --session <session-id> -- ls -laThis runs the command within the session's sandboxed root directory on the session's host.
Flags Reference
| Flag | Short | Description | Default |
|---|---|---|---|
--target | -t | Target selector (host:<id>, group:<name>, all) | — |
--host | -H | Single host by name (shorthand) | — |
--session | -s | Execute within a session context | — |
--timeout | -T | Per-host timeout | From config (typically 30m) |
--concurrency | -c | Max parallel hosts | From config (typically 50) |
--fail-fast | Stop on first failure | false | |
--min-success-rate | Minimum success rate (0.0-1.0) | — | |
--dry-run | -n | Preview targets and command without executing | false |
--rerun | Re-run the last multi-host execution on failure or success hosts | — | |
--when-online | Queue the command for offline hosts | false | |
--queue-ttl | TTL for queued commands created by --when-online | 24h | |
--batch-size | Execute N hosts at a time | 0 (all at once) | |
--batch-wait | Pause between batches | — | |
--subset | Execute on a random subset of N hosts | 0 | |
--stream | Stream per-host output live in human mode | false | |
--log-dir | Write per-host output to separate files | — | |
--out-file | Also write aggregate output to a file | — | |
--notify | Desktop notification on completion | false | |
--yes | -y | Skip the standard multi-host confirmation prompt | false |
--output | -o | Output format: human, json, jsonl, yaml, compact | human |
Controlled Rollouts
Use --batch-size and --batch-wait for staged execution across larger fleets:
nefia exec --target group:all --batch-size 5 --batch-wait 10s -- deploy.shUse --subset when you want to test a command on a random sample before a broader rollout:
nefia exec --target group:all --subset 3 -- uptimeOffline Queueing and Reruns
Use --when-online to queue commands for hosts that are currently offline:
nefia exec --target group:laptops --when-online --queue-ttl 48h -- softwareupdate --install --allUse --rerun failure or --rerun success to re-target only the hosts from the most recent multi-host execution outcome.
Concurrency Control
By default, Nefia executes commands on up to 50 hosts in parallel. Use --concurrency (or -c) to adjust this:
# Run on 1 host at a time (serial execution)
nefia exec --target all --concurrency 1 -- apt update
# Run on 5 hosts in parallel
nefia exec --target all --concurrency 5 -- apt updateTimeout Management
Set a per-host timeout with --timeout (or -T). If a command exceeds this duration, it is killed and reported as a failure:
nefia exec --target all --timeout 60s -- apt upgrade -yThe default timeout comes from your configuration (typically 30 minutes). Supported units are s (seconds), m (minutes), and h (hours).
Output Formats
Control how results are displayed with --output (or -o):
The default format. Groups output by host with color-coded status indicators:
nefia exec --target all -o human -- df -h /[web-01] Filesystem Size Used Avail Use% Mounted on [web-01] /dev/sda1 50G 32G 16G 68% /
[web-02] Filesystem Size Used Avail Use% Mounted on [web-02] /dev/sda1 50G 28G 20G 59% /
2/2 hosts succeeded
Fail-Fast Mode
By default, Nefia continues executing on remaining hosts even if some fail. Enable --fail-fast to abort immediately after the first failure:
nefia exec --target group:webservers --fail-fast -- nginx -t[web-01] nginx: the configuration file /etc/nginx/nginx.conf syntax is ok [web-02] nginx: [emerg] unexpected "}" in /etc/nginx/nginx.conf:42 FAIL (exit code 1)
Aborted: fail-fast triggered after web-02 failure 1/3 hosts succeeded, 1 failed, 1 skipped
Minimum Success Rate
For operations where partial failure is acceptable, set a minimum success threshold with --min-success-rate. The command reports success only if the percentage of successful hosts meets or exceeds this value:
nefia exec --target all --min-success-rate 0.8 -- systemctl restart appThis is useful in large fleets where a handful of transient failures are expected. If fewer than 80% of hosts succeed, the overall exit code is non-zero.
Progress Reporting
When executing commands across multiple hosts, Nefia displays real-time progress in your terminal.
Multi-host operations show a live progress indicator with success and failure counts:
[3/10] 2 succeeded 1 failed running...
When all hosts complete, a summary is printed:
10 hosts: 8 succeeded, 2 failed (5.2s)
Desktop Notifications
Add the --notify flag to receive a desktop notification when the operation completes:
nefia exec --target all --notify -- apt updateYou can also enable automatic notifications for long-running operations in your configuration:
defaults:
notifications:
enabled: true
min_duration: "30s"With this configuration, any operation taking longer than 30 seconds automatically triggers a notification without the --notify flag.
| Platform | Mechanism | Requirement |
|---|---|---|
| macOS | osascript (AppleScript) | None (built-in) |
| Linux | notify-send | libnotify package |
| Windows | PowerShell toast | None (built-in) |
Practical Examples
Rolling restart across a group
nefia exec --target group:webservers --concurrency 1 --timeout 60s -- systemctl restart nginxCheck disk usage on all hosts with JSON output
nefia exec --target all -o json -- df -h / | jq '.results[] | "\(.host): \(.output)"'Run a command on a specific group
nefia exec --target group:dev-team -- df -hValidate configuration before deploying
nefia exec --target group:webservers --fail-fast -- nginx -t && \
nefia exec --target group:webservers --concurrency 1 -- systemctl reload nginxRelated
Complete reference for all exec flags and options.
Read, write, and sync files across remote hosts.
Automate multi-step operations with YAML playbooks.