Reboot or shut down devices with Azure IoT and OSConfig

Important

Version 1.0.3 (published 28 June 2022) includes breaking changes to member names which may impact existing users. For more information, see: Member names transition from PascalCase to camelCase in version 1.0.3

Audience and scope

This article is designed to support people who provision or manage devices with Azure IoT. If that doesn't sound like you, consider taking a look at Audiences for OSConfig documentation.

This article is about how to reboot or shutdown devices using the CommandRunner feature of OSConfig. This article focuses on practical examples with minimal explanation. For technical background on the CommandRunner feature, the interaction model, etc. see How to interact with the CommandRunner feature of OSConfig and Azure IoT.

Use-case examples

Prerequisites to try the examples on live systems

If you are using this article for reference (for example, you are here to copy a property name), there are no pre-requisites.

If you want to try the examples on live systems (recommended), then:

  1. You will need an Azure account with an IoT Hub

    This article assumes some familiarity with IoT Hub and related tools. For example, it assumes you are comfortable creating IoT Hubs and attaching devices. If you prefer a more prescriptive step-by-step introduction to installing and using OSConfig from scratch, see: Quickstart: Manage a single virtual IoT device using Azure CLI instead.

  2. You will need at least one Linux device with the OSConfig agent installed and connected to Azure IoT.

    For more information, see: How and where to install the OSConfig agent for Linux.

  3. You will use Azure Portal or Azure CLI to interact with the devices via your IoT Hub

    For further steps, choose your preferred experience:

  1. Ensure you are signed in to the Azure Portal and can access your IoT Hub's Overview page Screenshot showing IoT Hub and devices from the Azure Portal

Example A. Reboot one or several devices

For the single device examples, you can add the reboot directive directly to the OSConfig twin of the device you wish to reboot.

For the at-scale examples, we imagine working with an outside process which is adding and removing needsReboot tags to devices. We will create an IoT Hub Configuration which dynamically targets devices where the needsReboot tag is set in the osconfig twin. We will simulate this outside process by explicitly setting the tag.

  1. From your IoT Hub's portal page, navigate to the OSConfig twin for the device you wish to manage, and add the following to the properties.desired section, followed by a comma to separate it from the next item in properties.desired. action=1 below specifies reboot.

    "CommandRunner": {
       "__t": "c",
       "commandArguments": {
          "commandId": "reboot_single_device_cmd",
          "arguments": "",
          "timeout": 30,
          "singleLineTextResult": false,
          "action": 1
       }
    }
    

    Screen capture showing how to reboot a specific device using OSConfig module from Azure Portal

  2. Optional: Once the device is back online (this will take a few minutes), you can check the properties.reported section and check for the commandStatus that will show reboot action completed successfully, as shown below.

    "CommandRunner": {
       "__t": "c",
       "commandStatus": {
          "commandId": "reboot_single_device_cmd",
          "resultCode": 0,
          "textResult": "",
          "currentState": 2
       }
    }
    

1.Optional: You can also log into your device or VM to validate after the device has rebooted.

Example B. Shutdown instead of reboot

Remotely shutting down (as opposed to rebooting) edge/IoT devices is a less common operation, due the the device being subsequently unavailable (barring an out-of-band way to boot it back up). Even so, there are real-world cases which call for remote shut down. Imagine a problem device which is sending corrupt data, is over-consuming precious satellite transmission bytes, is believed to be compromised, or is otherwise doing more harm than good. In such cases it can be desirable to shut down the device until it can be replaced or repaired.

To perform shutdown of devices through OSConfig, you can use the same cloud workflows as Example A, but using "action": 2 (shutdown) rather than "action": 1 (reboot) in commandArguments. Please be aware that each request in CommandRunner needs a new commandId value. For example, if you already performed a reboot in Example A and now want to switch to shutdown, you will need to modify commandArguments not just with "action": 2 but also with a not-previously-used value for commandId, such as "commandId": "my_shutdown_cmd". For more on the CommandRunner interaction model, see: How to interact with the CommandRunner feature of OSConfig and Azure IoT.

Next steps

For an overview of OSConfig scenarios and capabilities, see:

For specific practical examples, see: