Skip to content

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:

bash
nefia exec [flags] -- <command>

Run a command on a single host using the --host (or -H) shorthand:

bash
nefia exec --host pc-office -- hostname
nefia exec --host pc-office -- hostname

[pc-office] pc-office.local

To run on all connected hosts, use --target all:

bash
nefia exec --target all -- uptime
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.

SelectorSyntaxExample
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
  • ~regex for host ID regex matching
  • @path/to/file to read host IDs from a file
  • - to read host IDs from stdin
bash
# 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-' -- hostname

Session-Scoped Execution

Use --session (or -s) to execute a command within an existing session context:

bash
nefia exec --session <session-id> -- ls -la

This runs the command within the session's sandboxed root directory on the session's host.

Flags Reference

FlagShortDescriptionDefault
--target-tTarget selector (host:<id>, group:<name>, all)
--host-HSingle host by name (shorthand)
--session-sExecute within a session context
--timeout-TPer-host timeoutFrom config (typically 30m)
--concurrency-cMax parallel hostsFrom config (typically 50)
--fail-fastStop on first failurefalse
--min-success-rateMinimum success rate (0.0-1.0)
--dry-run-nPreview targets and command without executingfalse
--rerunRe-run the last multi-host execution on failure or success hosts
--when-onlineQueue the command for offline hostsfalse
--queue-ttlTTL for queued commands created by --when-online24h
--batch-sizeExecute N hosts at a time0 (all at once)
--batch-waitPause between batches
--subsetExecute on a random subset of N hosts0
--streamStream per-host output live in human modefalse
--log-dirWrite per-host output to separate files
--out-fileAlso write aggregate output to a file
--notifyDesktop notification on completionfalse
--yes-ySkip the standard multi-host confirmation promptfalse
--output-oOutput format: human, json, jsonl, yaml, compacthuman

Controlled Rollouts

Use --batch-size and --batch-wait for staged execution across larger fleets:

bash
nefia exec --target group:all --batch-size 5 --batch-wait 10s -- deploy.sh

Use --subset when you want to test a command on a random sample before a broader rollout:

bash
nefia exec --target group:all --subset 3 -- uptime

Offline Queueing and Reruns

Use --when-online to queue commands for hosts that are currently offline:

bash
nefia exec --target group:laptops --when-online --queue-ttl 48h -- softwareupdate --install --all

Use --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:

bash
# 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 update

Timeout Management

Set a per-host timeout with --timeout (or -T). If a command exceeds this duration, it is killed and reported as a failure:

bash
nefia exec --target all --timeout 60s -- apt upgrade -y

The 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:

bash
nefia exec --target all -o human -- df -h /
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:

bash
nefia exec --target group:webservers --fail-fast -- nginx -t
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:

bash
nefia exec --target all --min-success-rate 0.8 -- systemctl restart app

This 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:

nefia exec --target all -- apt update

[3/10] 2 succeeded 1 failed running...

When all hosts complete, a summary is printed:

Batch summary

10 hosts: 8 succeeded, 2 failed (5.2s)

Desktop Notifications

Add the --notify flag to receive a desktop notification when the operation completes:

bash
nefia exec --target all --notify -- apt update

You can also enable automatic notifications for long-running operations in your configuration:

yaml
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.

PlatformMechanismRequirement
macOSosascript (AppleScript)None (built-in)
Linuxnotify-sendlibnotify package
WindowsPowerShell toastNone (built-in)

Practical Examples

Rolling restart across a group

bash
nefia exec --target group:webservers --concurrency 1 --timeout 60s -- systemctl restart nginx

Check disk usage on all hosts with JSON output

bash
nefia exec --target all -o json -- df -h / | jq '.results[] | "\(.host): \(.output)"'

Run a command on a specific group

bash
nefia exec --target group:dev-team -- df -h

Validate configuration before deploying

bash
nefia exec --target group:webservers --fail-fast -- nginx -t && \
  nefia exec --target group:webservers --concurrency 1 -- systemctl reload nginx
CLI Reference

Complete reference for all exec flags and options.

File Operations

Read, write, and sync files across remote hosts.

Playbooks

Automate multi-step operations with YAML playbooks.