> 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/automated-tasks.md). Agents can also search and read this documentation through its [MCP server](https://wiki.datadike.com/~gitbook/mcp).

# Automated tasks

Schedule scripts and commands on a device or through a policy: triggers, actions, collector tasks, run now, how Windows and Linux/macOS schedule them, and where the results appear.

## What this is and when to use it

An automated task is a list of actions (library scripts and command lines) that SONAR runs on a device on a trigger: a schedule, the first contact of a new device, the failure of a check, or on demand. A task can also be a **collector task**, which writes its output into a custom field.

Tasks live in two places:

* on one device, in its **TASKS** tab;
* in an [automation policy](https://wiki.datadike.com/sonar/administration/automation/automation-policies.md), in the policy's **Tasks** tab, which applies the task to every device that inherits the policy.

Use tasks for recurring maintenance (clean-ups, reports, inventory values), for first-run configuration of new devices, and for automatic remediation when a check fails.

## Before you start

* A role with **List Tasks** to see tasks, **Manage Tasks** to create, edit and delete them, and **Run Tasks** to use **Run task now**. They are in the **Automation & Tasks** group of **Admin Center › Roles**.
* Access to the device's client or site, for device tasks.
* At least one script in **Management › Scripts**. The task form does not open its steps while the library is empty.
* For a collector task, the custom field created in **Admin Center › Settings › Custom Fields** (see [Custom fields](https://wiki.datadike.com/sonar/administration/settings/custom-fields.md)).
* For **On check failure**, the check already configured on the device or in the policy (see [Checks](https://wiki.datadike.com/sonar/administration/automation/checks.md)).

## How tasks run on each platform

|                                                | Windows                                                                                                                         | Linux and macOS                                                                                                                       |
| ---------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| Where the schedule lives                       | On the device, as a scheduled task that the agent creates in the Windows Task Scheduler                                         | On the SONAR server, which checks every minute which tasks are due and tells the agent to run them                                    |
| When a change reaches the device               | When the device syncs with the server; the **Sync Status** column shows the progress                                            | Immediately, on the next due run                                                                                                      |
| If the device is offline at the scheduled time | The Windows scheduler handles the missed start; **Run task ASAP after a scheduled start is missed** runs it as soon as possible | The run for that time is not delivered; the task runs at its next scheduled time                                                      |
| Time used                                      | The device's time zone                                                                                                          | The device's time zone. Only the hour and minute of **Run at** are used, except for **Run Once**                                      |
| Schedule options                               | All options, including **Advanced Settings (Windows only)** and **Expires on**                                                  | All run types. **Daily**, **Weekly** and **Monthly** use the time of day and the chosen days only: no intervals, repetition or expiry |

On every platform, actions run with the agent's privileges. On Windows, a scheduled task runs as SYSTEM; a script marked **Run As User (Windows only)** in the library runs as the logged-in user instead.

## Steps

### Add a task to a device

1. Open **Dashboard › Sysadmin**, select the device and open its **TASKS** tab.
2. Click **Add Task**. **Adding Automated Task** opens on step **Select Task**.
3. Type the **Descriptive name of task**.
4. For a collector task, tick **Collector Task**, choose the **Custom Field to update** and, to store the whole output instead of the last line, tick **Save all output**.
5. Choose the **Alert Severity** used when the task fails, and click **Next**.
6. In **Configure Actions**, choose **Script** or **Command** under **Action Type**:
   * **Script**: pick it in **Select script**, review **Script Arguments**, **Environment vars** and **Timeout (seconds)**, then click **Add**.
   * **Command**: type the **Command**, set **Timeout (seconds)**, choose **CMD**, **Powershell**, **Bash** or **Custom** (with **Custom shell**), then click **Add**.
7. Repeat for each action. Drag the handles to reorder them; they run from top to bottom. Untick **Continue on Errors** if the task must stop at the first action that fails. Click **Next**.
8. In **Choose Schedule**, pick the **Task run type** and fill its fields (see the field reference).
9. Click **Add Task**.

### Add a task to a policy

1. Open the **Management** menu in the header and choose **Automations**.
2. Select the policy and open its **Tasks** tab, then add the task. The form is the same, with one more field on the first step: **Supported Platforms**, the platforms the task applies to.
3. Every device that inherits the policy and runs one of those platforms receives the task. See [Automation policies](https://wiki.datadike.com/sonar/administration/automation/automation-policies.md).

### Run a task now

1. In the device's **TASKS** tab, right-click the task.
2. Choose **Run task now**. The confirmation reads `<task> will now be run.`

This works for device tasks and for policy tasks shown on the device, and for any trigger, including **Manual**. A disabled task must be enabled first. To run a policy task on all its devices at once, use **Run** in the policy's **Tasks** tab.

### Enable, disable, edit or delete

* Tick or untick the power column to enable or disable a device task.
* Double-click a device task, or right-click it and choose **Edit**, to change it. The change reaches Windows devices when they sync.
* Right-click and choose **Delete**. The task is removed from the device shortly after.

Tasks that come from a policy show the policy icon (`This task is managed by a policy`) and are edited in the policy, not on the device.

### Run a task when a check fails

1. Create or open the task and, in **Choose Schedule**, pick **On check failure**.
2. In **Select Check**, choose the check.
3. Save. Each time that check reports a failure, the device runs the task. The task runs on every failing result, not only the first, so write its actions so that running them again does no harm.

## What you should see

The task appears in the **TASKS** tab with its **Name**, **Sync Status**, **More Info**, **Last Run Time**, **Schedule** and, for check-failure tasks, **Assigned Check**. The first columns show whether it is enabled, its SMS, email and dashboard alert settings, the policy icon, the collector icon and its status: passing, or the failure severity (**Informational**, **Warning**, **Error**).

**Sync Status** reads **Synced with agent**, **Will sync on next agent checkin**, **Waiting for task creation on agent** or **Pending deletion on agent**.

## How to verify it worked

1. In the **TASKS** tab, click the refresh button after the scheduled time or after **Run task now**. **Last Run Time** shows the run, and **More Info** changes from **Awaiting output** to **output**.
2. Click **output**. **Script Output** shows **Last Run**, **Run Time**, **Return Code**, **Standard Output** and **Standard Error**. A return code other than 0 marks the task as failing; so does any standard error output on a collector task.
3. Open the **HISTORY** tab. Each run adds a **Task Run** line.
4. For a collector task, open the device with **Edit** and check the custom field.
5. For a policy task, use **See Status** in the policy's **Tasks** tab to see the result on each device.

## Common errors

| Symptom                                                                                                | Cause                                                    | Fix                                                                             |
| ------------------------------------------------------------------------------------------------------ | -------------------------------------------------------- | ------------------------------------------------------------------------------- |
| The form shows a short message and a link instead of the three steps                                   | The script library is empty                              | Add a script in **Management › Scripts** first                                  |
| `There must be at least one supported platform`                                                        | A policy task with no platform ticked                    | Tick at least one platform                                                      |
| `Script and timeout must be set` / `A command and timeout must be set`                                 | **Add** clicked with an empty script, command or timeout | Fill them before **Add**                                                        |
| `There must be at least one action`                                                                    | **Next** on **Configure Actions** with an empty list     | Add at least one action                                                         |
| `Expires date/time is in the past`                                                                     | **Expires on** is earlier than now                       | Choose a future date or clear the field                                         |
| `Task cannot be run when it's disabled. Enable it first.`                                              | **Run task now** on a disabled task                      | Enable the task                                                                 |
| **Sync Status** stays at **Will sync on next agent checkin** or **Waiting for task creation on agent** | A Windows device has not synced yet, or is offline       | Wait for the device to come online; the status changes to **Synced with agent** |
| A Linux or macOS task did not run at its time                                                          | The device was offline at that minute                    | Keep the device online at the scheduled time, or use **Run task now**           |
| **Edit** and **Delete** are missing from the menu                                                      | The task comes from a policy                             | Change it in **Management › Automations**                                       |

## Field reference

### Select Task

| Field                    | What it does                                                                                                       | Accepted values                           | Default                                |
| ------------------------ | ------------------------------------------------------------------------------------------------------------------ | ----------------------------------------- | -------------------------------------- |
| Descriptive name of task | Name shown in the tabs and in **History**                                                                          | Text                                      | Empty. Required                        |
| Supported Platforms      | Policy tasks only. Platforms that receive the task                                                                 | **Windows**, **Linux**, **macOS**         | **Windows**                            |
| Collector Task           | Writes the output to a custom field                                                                                | On or off                                 | Off                                    |
| Custom Field to update   | Destination of a collector task. The value is stored on the device that ran the task, so use a device custom field | A device custom field                     | None. Required with **Collector Task** |
| Save all output          | Stores the whole output; otherwise only the last line                                                              | On or off                                 | Off                                    |
| Alert Severity           | Severity of the alert raised when the task fails                                                                   | **Informational**, **Warning**, **Error** | **Informational**                      |

### Configure Actions

| Field              | What it does                                         | Accepted values                                                                              | Default                     |
| ------------------ | ---------------------------------------------------- | -------------------------------------------------------------------------------------------- | --------------------------- |
| Action Type        | Kind of action to add                                | **Script**, **Command**                                                                      | **Script**                  |
| Select script      | Library script to run                                | Any library script                                                                           | None                        |
| Script Arguments   | Arguments for this action                            | Text, with [script variables](https://wiki.datadike.com/sonar/reference/script-variables.md) | The script's defaults       |
| Environment vars   | Environment variables, as `KEY=VALUE`                | Text, with script variables                                                                  | The script's defaults       |
| Command            | Command line to run. Can contain one script variable | Text                                                                                         | Empty                       |
| Shell              | Interpreter of a command action                      | **CMD**, **Powershell**, **Bash**, **Custom**                                                | **CMD**                     |
| Custom shell       | Interpreter path for **Custom**                      | Absolute path                                                                                | Empty                       |
| Timeout (seconds)  | Maximum run time of the action                       | Whole number                                                                                 | 30, or the script's default |
| Continue on Errors | Keeps running the next actions when one fails        | On or off                                                                                    | On                          |

### Choose Schedule

| Field                                           | What it does                                                                                                                | Accepted values                                                                                    | Default             |
| ----------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- | ------------------- |
| Task run type                                   | The trigger                                                                                                                 | **Daily**, **Weekly**, **Monthly**, **Run Once**, **On check failure**, **Onboarding**, **Manual** | **Daily**           |
| Start time / Run at                             | First run (Windows) or time of day (Linux and macOS), in the device's time zone. For **Run Once**, it must be in the future | Date and time                                                                                      | Now                 |
| Expires on                                      | Windows only. Date after which the task no longer runs                                                                      | Date and time, in the future                                                                       | Empty               |
| Run every … days                                | **Daily**, Windows only                                                                                                     | 1 to 255                                                                                           | 1                   |
| Run every … weeks                               | **Weekly**, Windows only                                                                                                    | 1 to 52                                                                                            | 1                   |
| Run on Days                                     | **Weekly**: days of the week                                                                                                | Monday to Sunday                                                                                   | None. Required      |
| On Days / On Weeks                              | **Monthly**: by day of the month or by week of the month                                                                    | Choice                                                                                             | **On Days**         |
| Run on Months                                   | **Monthly**: months to run in, with **All months**                                                                          | January to December                                                                                | None. Required      |
| Run on Days                                     | **Monthly › On Days**: days of the month, with **All days** and **Last Day**                                                | 1 to 31, **Last Day**                                                                              | None. Required      |
| Run on weeks / Run on days                      | **Monthly › On Weeks**: which weeks and which weekdays                                                                      | **First Week** to **Fourth Week**, **Last Week**; Monday to Sunday                                 | None. Required      |
| Select Check                                    | **On check failure**: the check that triggers the task                                                                      | Checks of the device or policy                                                                     | None. Required      |
| Repeat task every                               | Windows only. Repeats the task within the day                                                                               | 1 to 3 digits plus `d`, `h`, `m` or `s`, for example `30m`                                         | Empty               |
| Task repeat duration                            | Windows only. How long the repetition lasts. Must be longer than the interval                                               | Same format, for example `6h`                                                                      | Empty               |
| Stop all tasks at the end of duration           | Windows only. Stops running instances when the duration ends                                                                | On or off                                                                                          | Off                 |
| Random task delay                               | Windows only. Random delay added to each start                                                                              | Same format, for example `2m`                                                                      | Empty               |
| Delete task if not scheduled for 30 days        | Windows only. Needs **Expires on**                                                                                          | On or off                                                                                          | Off                 |
| Run task ASAP after a scheduled start is missed | Windows only. Not available for **Run Once**                                                                                | On or off                                                                                          | On                  |
| Task instance policy                            | Windows only. What happens when a run starts while the previous one is still running                                        | **Run in Parallel**, **Queue Task**, **Ignore**, **Stop Existing**                                 | **Run in Parallel** |

**Onboarding** runs once, as soon as the task reaches a device, which makes it the trigger for first-run configuration in a policy. **Manual** never runs on its own; it runs only with **Run task now** or the policy's **Run**.

## Related pages

* [Automation policies](https://wiki.datadike.com/sonar/administration/automation/automation-policies.md)
* [Checks](https://wiki.datadike.com/sonar/administration/automation/checks.md)
* [Script library](https://wiki.datadike.com/sonar/administration/automation/scripts.md)
* [Alerts and alert templates](https://wiki.datadike.com/sonar/administration/automation/alerts.md)
* [Script variables](https://wiki.datadike.com/sonar/reference/script-variables.md)
