Skip to main content
Documentation
Preview
You're viewing the Next docs — a rolling preview of in-development changes. The current release docs may differ.

Create a Device Command

Add a command that operators can run on a device from Nexigon, here one that reports the device’s uptime. A command consists of a TOML definition, which operators see, and an executable handler, which does the work with the device’s own tools. Nexigon Agent publishes the definition to Nexigon Hub and runs the handler when an authorized client invokes the command.

You need root access to a Linux device that runs Nexigon Agent with systemd and Bash.

Write the Handler

Create the directories for handlers and definitions:

install -d -m 755 /usr/libexec/nexigon/commands /etc/nexigon/agent/commands

Save this script and make it executable with chmod 755 /usr/libexec/nexigon/commands/uptime.sh:

/usr/libexec/nexigon/commands/uptime.sh
#!/usr/bin/env bash
set -euo pipefail

read -r uptime_secs _ </proc/uptime
printf '{"type":"Output","data":{"uptime_secs":%s}}\n' "${uptime_secs%.*}"

A handler receives its input as JSON on stdin and writes JSON records to stdout. The data of the Output record becomes the command’s result. Run the script directly to check that it prints this record. When you adapt the handler, write diagnostics to stderr, so that they do not corrupt the result, and exit with a non-zero status when the work fails, so that operators and fleet operations see the failure.

Define the Command

Save the definition in the agent’s commands directory:

/etc/nexigon/agent/commands/uptime.toml
[command]
name = "uptime"
description = "Get system uptime in seconds"
category = "diagnostics"

[exec]
handler = ["/usr/libexec/nexigon/commands/uptime.sh"]
timeout = 5

The agent kills the handler after five seconds. Handlers run with the agent’s privileges, so the agent loads a definition only if the definition, the handler, and their parent directories are owned by root or the agent’s user and not writable by others.

Enable and Run the Command

Enable commands in the agent’s configuration and restart the agent:

/etc/nexigon/agent.toml
[commands]
enabled = true
directory = "/etc/nexigon/agent/commands"

Open the device’s page in Nexigon and run uptime. The result contains uptime_secs. With Nexigon CLI, you can also list the commands that the device publishes:

nexigon-cli devices commands list <device-id>

With a user token, add --organization <org-id>, as Organization Selection describes. If the command is missing, journalctl -u nexigon-agent shows why the agent skipped it.

Device Commands describes all definition fields, input and output schemas, and the handler protocol.