> For the complete documentation index, see [llms.txt](https://wiki.datadike.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wiki.datadike.com/sonar/administration/automation/run-commands-and-scripts.md). Agents can also search and read this documentation through its [MCP server](https://wiki.datadike.com/~gitbook/mcp).

# Run a command or a script on a device

Send a shell command or run a library script on one device, choose what happens to the output, and find every run in the device's History tab.

## What this is and when to use it

**Send Command** runs a single command line on one device and shows the output in the same dialog. **Run Script** runs a script from the [script library](https://wiki.datadike.com/sonar/administration/automation/scripts.md) on one device, with its arguments, environment variables and timeout, and lets you decide what happens with the output: wait for it, let it run in the background, email it, store it in a custom field or save it as a device note.

Use them for one-off diagnosis and fixes on a single device. To reach many devices at once, use [bulk actions](https://wiki.datadike.com/sonar/administration/automation/bulk-actions.md); to repeat the same work on a schedule, use [automated tasks](https://wiki.datadike.com/sonar/administration/automation/automated-tasks.md).

## Before you start

* A role with **Send Command** for commands and **Run Scripts** for scripts, both in the **System** group of **Admin Center › Roles**. To read past runs, **List Agent History** (group **Agents**).
* Access to the device: the role must cover its client or site.
* The device must be online. Commands and scripts are delivered to the agent at the moment you click **Send** or **Run**; nothing is queued for a device that is offline.
* For **Run Script**, the script must exist in **Management › Scripts** and support the device's platform.
* For **Email results**, outgoing mail configured in **Admin Center › Settings** (see [Email and SMS notifications](https://wiki.datadike.com/sonar/administration/settings/email-and-sms.md)).

## Steps

### Send a command

1. Open **Dashboard › Sysadmin**, open the client and find the device. Open its action menu: right-click the device in the list, or select it and use ⋮ (**Other actions**) in the device panel.
2. Choose **Send Command**.
3. Under **Shell**, pick the interpreter. Windows devices offer **CMD** and **Powershell**; Linux and macOS devices offer **Bash** and **Custom**.
4. With **Custom**, type the interpreter path in **Custom shell**, for example `/usr/bin/python3`.
5. On Windows, tick **Run As User** to run the command in the context of the logged-in user.
6. Adjust **Timeout (seconds)** if the command needs more than the default 30.
7. Type the command in the terminal box on the left.
8. Click **Send**. The dialog waits for the command to end and shows the result in **Output**, on the right.

### Run a script

1. Open the device's action menu and choose **Run Script**.
2. In **Select script**, choose the script. Only scripts that support the device's platform are listed. Hover the ⓘ icon to see the script's syntax notes.
3. Review **Script Arguments** and **Environment vars**. They are filled with the script's defaults; change them for this run if needed. Both accept [script variables](https://wiki.datadike.com/sonar/reference/script-variables.md).
4. Choose what to do with the output (see the table below).
5. On Windows, tick **Run As User** if the script must run as the logged-in user.
6. Check **Timeout (seconds)**, filled with the script's default.
7. Click **Run**.

| Output option                    | What happens                                                                                                                                                                                     |
| -------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| **Wait for Output**              | The dialog waits and shows the output below the **Run** button                                                                                                                                   |
| **Fire and Forget**              | The dialog closes at once with `<script> will now be run on <device>`. Read the result later in **History**                                                                                      |
| **Email results**                | The script runs in the background and the result is emailed, either to **Use email addresses from global settings** or to the addresses in **Email recipients** when **Custom emails** is chosen |
| **Save results to Custom Field** | The dialog waits, then writes the last line of the output to the custom field chosen in **Select custom field**, or the whole output when **Save all output** is ticked                          |
| **Save results to Agent Notes**  | The dialog waits, then saves the output as a note on the device, visible in its **NOTES** tab                                                                                                    |

The email subject is `<client>, <site>, <device> <script> Results`, and the body carries the return code, the execution time, the standard output and the standard error.

A custom field used as the destination can belong to the device, to its site or to its client; the value is written on the matching record.

### Run a favorite script

1. Open the device's action menu and hover **Run Favorited Script**.
2. Pick a script from the submenu. It lists the scripts marked as favorites in the library.
3. **Run Script** opens with that script selected. Continue from step 3 above.

## What you should see

* **Send Command**: the output of the command in **Output**, with a copy button.
* **Run Script** with **Wait for Output**, **Save results to Custom Field** or **Save results to Agent Notes**: the output of the script below the **Run** button.
* **Fire and Forget** and **Email results**: a confirmation message; the output is not shown in the dialog.

## How to verify it worked

1. Open the device's **HISTORY** tab and click the refresh button. Every command and every script run from the console adds a line with **Time**, **Action** (**Cmd Run** or **Script Run**), **Script/Command**, **Initiated By** and **Output**. Automated tasks add **Task Run** lines to the same tab.
2. Click **Output** on the line. A command opens its text output; a script opens **Script Output**, with **Last Run**, **Run Time**, **Return Code**, **Standard Output** and **Standard Error**.
3. For **Save results to Custom Field**, open the device, site or client and check the value of the custom field. For **Save results to Agent Notes**, check the **NOTES** tab.
4. Open **Reports & Audits › Audits**. A command is recorded as **Execute Command** (`<user> issued <shell> command on <device>.`); a script as **Execute Script** (`<user> ran script: "<script>" on <device>`).

## Common errors

| Symptom                                                                                                                     | Cause                                                                                          | Fix                                                                                                          |
| --------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ |
| **Send Command**, **Run Script** or **Run Favorited Script** is greyed out with `You don't have permission for this action` | The role lacks **Send Command** or **Run Scripts**                                             | Add the permission to the role                                                                               |
| `Unable to contact the agent`                                                                                               | The device is offline or did not answer in time                                                | Check the device status. For long work, raise the timeout or run a script with **Fire and Forget**           |
| `You don't have any scripts favorited!`                                                                                     | No script is marked as favorite                                                                | Mark scripts with **Add as Favorite** in **Management › Scripts**                                            |
| The script is not in **Select script**                                                                                      | It does not support the device's platform, or it is hidden                                     | Edit **Supported Platforms**, or use **Show Script** in the library                                          |
| `Custom shell path is required when using Custom shell.`                                                                    | **Custom** was chosen with an empty path                                                       | Type the interpreter path                                                                                    |
| `Minimum is 10 seconds` / `Maximum is 3600 seconds` (**Send Command**), `Minimum is 5 seconds` (**Run Script**)             | The timeout is out of range                                                                    | Enter a value inside the range                                                                               |
| `No output is available yet` in **History**                                                                                 | The run has not reported back: it is still running, or the device went offline before it ended | Refresh the tab later. If the device was offline when the action was sent, run it again when it is online    |
| The script receives `{{...}}` literally                                                                                     | A variable in the arguments does not resolve                                                   | Check the spelling against [Script variables](https://wiki.datadike.com/sonar/reference/script-variables.md) |

## Field reference

### Send Command

| Field             | What it does                                                                                     | Accepted values                                                         | Default                                |
| ----------------- | ------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------- | -------------------------------------- |
| Shell             | Interpreter that runs the command                                                                | Windows: **CMD**, **Powershell**. Linux and macOS: **Bash**, **Custom** | **CMD** on Windows, **Bash** elsewhere |
| Custom shell      | Path of the interpreter when **Custom** is chosen                                                | An absolute path                                                        | Empty. Required with **Custom**        |
| Run As User       | Windows only. Runs in the context of the logged-in user; if no user is logged in, runs as SYSTEM | On or off                                                               | Off                                    |
| Timeout (seconds) | How long the agent lets the command run, and the dialog waits for it                             | Whole number, 10 or more                                                | 30                                     |
| Command           | The command line                                                                                 | Text                                                                    | Empty. Required                        |

### Run Script

| Field               | What it does                                                                                                  | Accepted values                                                                                                                | Default                         |
| ------------------- | ------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ | ------------------------------- |
| Select script       | The library script to run                                                                                     | Scripts supporting the device's platform                                                                                       | None. Required                  |
| Script Arguments    | Arguments for this run. Press Enter after each                                                                | Text, with script variables                                                                                                    | The script's defaults           |
| Environment vars    | Environment variables for this run, as `KEY=VALUE`                                                            | Text, with script variables                                                                                                    | The script's defaults           |
| Output              | What happens to the output                                                                                    | **Wait for Output**, **Fire and Forget**, **Email results**, **Save results to Custom Field**, **Save results to Agent Notes** | **Wait for Output**             |
| Email mode          | Recipients for **Email results**                                                                              | **Use email addresses from global settings**, **Custom emails**                                                                | Global settings                 |
| Email recipients    | Addresses for **Custom emails**. Press Enter after each                                                       | Email addresses                                                                                                                | Empty                           |
| Select custom field | Destination for **Save results to Custom Field**                                                              | A device, site or client custom field                                                                                          | None. Required with this option |
| Save all output     | Stores the whole output instead of the last line                                                              | On or off                                                                                                                      | Off                             |
| Run As User         | Windows only. Runs as the logged-in user. A script marked **Run As User** in the library always runs this way | On or off                                                                                                                      | Off                             |
| Timeout (seconds)   | Maximum run time                                                                                              | 5 or more                                                                                                                      | The script's default            |

## Related pages

* [Script library](https://wiki.datadike.com/sonar/administration/automation/scripts.md)
* [Run actions across many devices](https://wiki.datadike.com/sonar/administration/automation/bulk-actions.md)
* [Script variables](https://wiki.datadike.com/sonar/reference/script-variables.md)
* [Device details](https://wiki.datadike.com/sonar/administration/devices/device-details.md)
* [Audit trail](https://wiki.datadike.com/sonar/administration/audit/audit-trail.md)
