# Script

**URL:** <https://hub.mender.io/t/script/328>\
**Category:** Update Modules\
**Created:** [March 12, 2019, 9:06am UTC](https://hub.mender.io/t/script/328 "2019-03-12T09:06:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![lluiscampos](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/lluiscampos/32/112_2.png) [@lluiscampos](https://hub.mender.io/u/lluiscampos)\
**Post date:** [March 12, 2019, 9:06am UTC](https://hub.mender.io/t/script/328/1 "2019-03-12T09:06:43Z")

</div>

### Description

The Script Update Module allows to execute any general purpose script or binary on the target device. A Mender Artifact containing one or more executables (usually scripts) is sent to the device, where the Update Module will execute these during the `ArtifactInstall` state.

If multiple scripts are provided, they will be executed in alphabetical order on the device.

Example use-cases:

- Restart application into diagnostic mode
- Run diagnostics script
- Execute any other generic command

### Specification

| | |
| --- | --- |
| Module name | script |
| Supports rollback | no |
| Requires restart | no |
| Artifact generation script | no |
| Full operating system updater | no |
| Source code | [Update Module](https://github.com/mendersoftware/mender-update-modules/tree/master/script/module/script) |
| Maintainer | Community |

## Prepare the device

This section describes how to setup your target device, i.e. the device to be updated. This will also be referred to as the device environment.

All commands outlined in this section should be run in the device environment.

### Prerequisites

This update module has the following prerequisites for the device environment:

- [Install the Mender client](https://docs.mender.io/client-installation/install-with-debian-package), version 2.0 or later
- Ensure the device has a Bash Unix shell
  - How to install this depends on which OS you are running

### Install the Update Module

Download the latest version of this Update Module by running:

```bash
mkdir -p /usr/share/mender/modules/v3 && wget -P /usr/share/mender/modules/v3 https://raw.githubusercontent.com/mendersoftware/mender-update-modules/master/script/module/script

```

## Prepare the development environment on your workstation

This section describes how to set up your development environment on your workstation.

All commands outlined in this section should be run in the development environment.

### Prerequisites

This Update Modules has the following prerequisites for the development environment:

- [Install mender-artifact](https://docs.mender.io/downloads), version 3.1.0 or later

### Create Mender Artifacts

The Artifact can be generated using `mender-artifact` tool.

Create example content to deploy:

```bash
cat > my-script-1.sh << EOF
#!/bin/bash
echo "$(basename "$0") was executed on device!" > /tmp/my-script-1.txt
EOF

cat > my-script-2.sh << EOF
#!/bin/bash
echo "$(basename "$0") was executed on device!" > /tmp/my-script-2.txt
EOF

```

Now generate a Mender Artifact using the following command:

```bash
ARTIFACT_NAME="my-update-1.0"
DEVICE_TYPE="my-device-type"
OUTPUT_PATH="my-update-1.0.mender"
SHELL_SCRIPTS="my-script-1.sh my-script-2.sh"
mender-artifact write module-image -T script -n ${ARTIFACT_NAME} -t ${DEVICE_TYPE} -o ${OUTPUT_PATH} -f $(echo "$SHELL_SCRIPTS" | sed -e 's/ / -f /g')

```

- `ARTIFACT_NAME` - The name of the Mender Artifact
- `DEVICE_TYPE` - The compatible device type of this Mender Artifact
- `OUTPUT_PATH` - The path where to place the output Mender Artifact. This should always have a `.mender` suffix
- `SHELL_SCRIPTS` - The path to the script that you wish to deploy and execute on your device. **NOTE** that the extension does not matter.

You can either deploy this Artifact in managed mode with the Mender server (upload it under Releases in the server UI) or by using the Mender client only in [Standalone deployments](https://docs.mender.io/artifact-creation/standalone-deployment).

### Artifact technical details

The Mender Artifact used by this Update Module has a payload with as many files as scripts are contained in the Artifact.

The Mender Artifact contents will look like:

```bash
Updates:
  - Type: script
    Provides:
      rootfs-image.script.version: my-update-1.0
    Depends: {}
    Clears Provides: [rootfs-image.script.*]
    Metadata: {}
    Files:
      - checksum: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
        modified: 2024-11-07 13:16:32 +0100 CET
        name: my-script-1.sh
        size: 140
      - checksum: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
        modified: 2024-11-07 13:16:32 +0100 CET
        name: my-script-2.sh
        size: 139

```

---

<div class="post-metadata">

**Author:** ![mEK](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/mek/32/377_2.png) [@mEK](https://hub.mender.io/u/mEK)\
**Post date:** [September 27, 2019, 2:48pm UTC](https://hub.mender.io/t/script/328/3 "2019-09-27T14:48:01Z")

</div>

Hello and good work.  
I have been working on the mender for a while and have integrated my device (Variscite i.MX-6ul) into the mender. I am currently using mender-client-2.0.0 and I can see my device in demo server:

 ![mender-artifact-error2](https://canada1.discourse-cdn.com/flex036/uploads/mender/original/1X/6e0bc3abcb2b4d3f4661a633197308e723efdc83.png)  
That seems like that.

I tried to use the script update module to create and distribute Artifact and follow this document. I recently created an artifact named my-update-1.0 and uploaded it to my demo server. My device doesn’t see this artifact when I try to distribute it. What could this be about? As an example, let me show you:

 ![mender-artifact-errorrrrr](https://canada1.discourse-cdn.com/flex036/uploads/mender/original/1X/2e3a0c96becb8d38a4d846e909b76cab953c31f8.png)

How do I fix this? Why Artifact and my device don’t match?

---

<div class="post-metadata">

**Author:** ![mirzak](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/mirzak/32/2056_2.png) [@mirzak](https://hub.mender.io/u/mirzak)\
**Post date:** [September 27, 2019, 2:52pm UTC](https://hub.mender.io/t/script/328/4 "2019-09-27T14:52:06Z")

</div>

You need to change the following,

```auto
DEVICE_TYPE="my-device-type"

```

to

```auto
DEVICE_TYPE="imx6ul-var-dart"

```

Because that is what your device is called.

---

<div class="post-metadata">

**Author:** ![mEK](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/mek/32/377_2.png) [@mEK](https://hub.mender.io/u/mEK)\
**Post date:** [September 30, 2019, 5:55am UTC](https://hub.mender.io/t/script/328/5 "2019-09-30T05:55:26Z")

</div>

Thank you @mirzak . After correcting the device type variable, the update was performed and I found that it was successful through the demo server. But when I go into my device, I see that nothing has changed and my device still work with previous update information. We were expecting to see an executable script file running on our device with this update module (script), but we couldn’t. Also the artifact name is still the same. How do I observe this?

---

<div class="post-metadata">

**Author:** ![mEK](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/mek/32/377_2.png) [@mEK](https://hub.mender.io/u/mEK)\
**Post date:** [September 30, 2019, 7:05am UTC](https://hub.mender.io/t/script/328/6 "2019-09-30T07:05:21Z")

</div>

![script_artifact](https://canada1.discourse-cdn.com/flex036/uploads/mender/original/1X/ac1910f5c395fce4503e63fb7e76a515cbd0f259.png)

Where is the this script files in my device ?

---

<div class="post-metadata">

**Author:** ![lluiscampos](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/lluiscampos/32/112_2.png) [@lluiscampos](https://hub.mender.io/u/lluiscampos)\
**Post date:** [September 30, 2019, 7:41am UTC](https://hub.mender.io/t/script/328/7 "2019-09-30T07:41:17Z")

</div>

> [@mEK](#):
>
> Where is the this script files in my device ?

The scripts are not saved in your device. The `script` Update Module executes these scripts while updating (i.e. they are executed only once).

> [@mEK](#):
>
> Also the artifact name is still the same. How do I observe this?

That is not expected. If the Artifact was successfully installed (in this case, that will mean that the Update Module executed the two scripts) then the Artifact name should have been updated to the name of your last Artifact (`my-update-1.0` in your case).

My guess here is that an error ocurred during the execution of these scripts. You should be able to find the logs in the finished deployments tab in the ui.

---

<div class="post-metadata">

**Author:** ![mEK](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/mek/32/377_2.png) [@mEK](https://hub.mender.io/u/mEK)\
**Post date:** [September 30, 2019, 8:06am UTC](https://hub.mender.io/t/script/328/8 "2019-09-30T08:06:03Z")

</div>

> That is not expected. If the Artifact was successfully installed (in this case, that will mean that the Update Module executed the two scripts) then the Artifact name should have been updated to the name of your last Artifact ( `my-update-1.0` in your case).

I mean, I can’t see this artifact name on my device. It still appears as first-artifact-emre in / etc / mender / artifact\_info :

 ![script_artifact](https://canada1.discourse-cdn.com/flex036/uploads/mender/original/1X/6276af35a19f14847475255cce76fc641aaddbc1.png)

And this is my demo-server dashboard information:

 ![script_artifact_1](https://canada1.discourse-cdn.com/flex036/uploads/mender/original/1X/c60d6d0a8b3df773552eab4f31a2ea584dd10f70.png)

Is this normal? The version information on the device and the version information on the server look different.  
On device artifact\_info: first-artifact-emre  
On server artifact\_info: my-update-01

---

<div class="post-metadata">

**Author:** ![lluiscampos](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/lluiscampos/32/112_2.png) [@lluiscampos](https://hub.mender.io/u/lluiscampos)\
**Post date:** [September 30, 2019, 8:21am UTC](https://hub.mender.io/t/script/328/9 "2019-09-30T08:21:02Z")

</div>

> [@mEK](#):
>
> It still appears as first-artifact-emre in / etc / mender / artifact\_info :

I understand now 🙂

The /etc/mender/artifact\_info file is deprecated from Mender 2.0.x. To locally check the current artifact name, use `mender -show-artifact`

The server reports the correct version: `my-update-01`

---

<div class="post-metadata">

**Author:** ![mEK](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/mek/32/377_2.png) [@mEK](https://hub.mender.io/u/mEK)\
**Post date:** [September 30, 2019, 8:24am UTC](https://hub.mender.io/t/script/328/10 "2019-09-30T08:24:57Z")

</div>

Thank you so much for speedy reply. You really helped me. See you next question 🙂

---

<div class="post-metadata">

**Author:** ![PJK](https://avatars.discourse-cdn.com/v4/letter/p/bc79bd/32.png) [@PJK](https://hub.mender.io/u/PJK)\
**Post date:** [April 14, 2020, 12:11pm UTC](https://hub.mender.io/t/script/328/11 "2020-04-14T12:11:12Z")

</div>

Hi,

I’m looking into this functionality because we need to wipe data from our devices before handing them over to new clients. This must be done separately from any firmware update.

Is it possible to use this mechanism in order to wipe data from a separate data partition (call it partition 5 in the boot/rootfsA/rootfsB/data setup) without having to switch to a new rootfs partition?

Firmware updates can happen while clients are using the device, so we don’t want to tie the data wipe to a firmware update. We really need it as a separate, remote startable action.

Could this mechanism be used for that?

The documentation isn’t quite clear on this topic, that’s why I’m asking.

So to recap:

Can a script be readied for the device to perform a remote data wipe (for instance a format of a separate client data partition) without triggering a complete firmware update involving a rootfs partition switch?

---

<div class="post-metadata">

**Author:** ![mirzak](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/mirzak/32/2056_2.png) [@mirzak](https://hub.mender.io/u/mirzak)\
**Post date:** [April 14, 2020, 12:53pm UTC](https://hub.mender.io/t/script/328/12 "2020-04-14T12:53:57Z")

</div>

> [@PJK](#):
>
> Can a script be readied for the device to perform a remote data wipe (for instance a format of a separate client data partition) without triggering a complete firmware update involving a rootfs partition switch?

Yes. The `script` Update Module will not trigger a switch of the rootfs and only implements the `ArtifactInstall` state.

---

<div class="post-metadata">

**Author:** ![PJK](https://avatars.discourse-cdn.com/v4/letter/p/bc79bd/32.png) [@PJK](https://hub.mender.io/u/PJK)\
**Post date:** [April 14, 2020, 1:05pm UTC](https://hub.mender.io/t/script/328/13 "2020-04-14T13:05:54Z")

</div>

Hi mirzak,

Thanks for the quick response. That really helps me enormously.

---

<div class="post-metadata">

**Author:** ![nishad1092](https://avatars.discourse-cdn.com/v4/letter/n/5fc32e/32.png) [@nishad1092](https://hub.mender.io/u/nishad1092)\
**Post date:** [June 15, 2020, 12:01pm UTC](https://hub.mender.io/t/script/328/14 "2020-06-15T12:01:11Z")

</div>

Hi,

I would like to use this module to reboot my RPi , I tried including “sudo reboot”, it reboot just fine, but deployment is failing in the mid way, because the mender artifact isn’t able to recognize the poweroff maybe, So whats the right way to reboot the device, Thats it, Only a reboot device command.,

---

<div class="post-metadata">

**Author:** ![lluiscampos](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/lluiscampos/32/112_2.png) [@lluiscampos](https://hub.mender.io/u/lluiscampos)\
**Post date:** [June 16, 2020, 9:59am UTC](https://hub.mender.io/t/script/328/15 "2020-06-16T09:59:28Z")

</div>

Hi @nishad1092 ,

Recently I realized that the this Update Module is not the best for the reboot device kind of use case. (I did update the description yesterday). The problem is that Mender must have full control on when to reboot or not the device; and doing it from an script is a bad practice. I am sorry for the confusion.

You can achieve this writing your own simple Update Module which returns “Yes” to “NeedsArtifactReboot”, and then use dummy Artifacts with no payload. Read more about this [in the spec documentation](https://github.com/mendersoftware/mender/blob/master/Documentation/update-modules-v3-file-api.md#artifactreboot-state).

---

<div class="post-metadata">

**Author:** ![nishad1092](https://avatars.discourse-cdn.com/v4/letter/n/5fc32e/32.png) [@nishad1092](https://hub.mender.io/u/nishad1092)\
**Post date:** [June 16, 2020, 11:02am UTC](https://hub.mender.io/t/script/328/16 "2020-06-16T11:02:47Z")

</div>

Sure @lluiscampos, Ill look into it, But I also heard from other member that if I use NeedsArtifactReboot Update Module, then it changes the Artifact name, Is it true? Becuase that can lead to unlikely situations in the future.

---

<div class="post-metadata">

**Author:** ![lluiscampos](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/lluiscampos/32/112_2.png) [@lluiscampos](https://hub.mender.io/u/lluiscampos)\
**Post date:** [June 16, 2020, 11:15am UTC](https://hub.mender.io/t/script/328/17 "2020-06-16T11:15:33Z")

</div>

@nishad1092 Yes. After you successfully “install” anything with Mender the current Artifact in the device will be updated.

Keep in mind that this remote control is a use case for what Mender is not intended for; hence the oddities with the Artifact name for example. Mender is intended for OTA updates in a robust and secure manner.

---

<div class="post-metadata">

**Author:** ![nishad1092](https://avatars.discourse-cdn.com/v4/letter/n/5fc32e/32.png) [@nishad1092](https://hub.mender.io/u/nishad1092)\
**Post date:** [June 23, 2020, 5:31am UTC](https://hub.mender.io/t/script/328/18 "2020-06-23T05:31:22Z")

</div>

Hi @lluiscampos,

Im using this module to execute python scripts.  
So, the issue is, When a deployment fails, the current release becomes “Inconsistent”, I understand that, but after it fails, the device doesnt sync up to Mender, I had to reboot the device manually, and further I cant deploy any releases before the previous release became Inconsistent.

I think, to resolve this, this module should have rollback feature ? is it?

Im using the default script module :—  
#!/bin/sh

set -e

STATE="$1"  
FILES="$2"

case “$STATE” in  
ArtifactInstall)  
for file in “$FILES”/files/\*; do  
chmod u+x “$file”  
“$file”  
done  
;;  
esac  
exit 0

How can I include rollback feature, so that if any update thru script fails, I need to rollback and also restart, Please could you give an example. I think ArtifactRollback or ArtifactRollbackReboot will help, Isn’t it? plz give an example, a simple rollback state and restart,

---

<div class="post-metadata">

**Author:** ![lluiscampos](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/lluiscampos/32/112_2.png) [@lluiscampos](https://hub.mender.io/u/lluiscampos)\
**Post date:** [June 23, 2020, 12:07pm UTC](https://hub.mender.io/t/script/328/19 "2020-06-23T12:07:15Z")

</div>

To add rollback to a given update module:

```bash
[...]
case "$STATE" in

    SupportsRollback)
        echo "Yes"
    ;;

    ArtifactRollback)
        # Here your rollback logic
    ;;
esac

exit 0

```

If you can share more details on what your Python script is doing I could give some advise on what “rolling back” could mean in your context.

Lluís

---

<div class="post-metadata">

**Author:** ![nishad1092](https://avatars.discourse-cdn.com/v4/letter/n/5fc32e/32.png) [@nishad1092](https://hub.mender.io/u/nishad1092)\
**Post date:** [June 23, 2020, 2:42pm UTC](https://hub.mender.io/t/script/328/20 "2020-06-23T14:42:16Z")

</div>

Hi @lluiscampos,  
Thank you for your response, I wish I can give more details but the script is confidential, But I can say that the script includes Installation of Python, docker, and creating files, folders, moving folders etc. So, I just want to know what does rollback offer?

Scenario: Say I have a folder /home/scripts/script1.py ( only 1 file is there currently) and I’m executing this .py which generates many other files (xxx.txt, abc.py, def.py) inside /home/scripts/ when I execute script1.py ,  
But if something goes wrong or deployment fails, I would like to rollback to the situation where I have only /home/scripts/script1.py (No files or folder, No .xxx.txt, or .py generated)…

@lluiscampos, The script has only command to execute the python file (just pythoin3 /home/.script/script1.py) , I’m using the directory module to deploy that script1.py into /home/scripts/)

---

<div class="post-metadata">

**Author:** ![lluiscampos](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/lluiscampos/32/112_2.png) [@lluiscampos](https://hub.mender.io/u/lluiscampos)\
**Post date:** [June 24, 2020, 6:32am UTC](https://hub.mender.io/t/script/328/21 "2020-06-24T06:32:46Z")

</div>

> [@nishad1092](#):
>
> So, I just want to know what does rollback offer?

That is the key: the rollback mechanism for Update Modules, where applicable, has to be implemented by the Update Moudule itself. Mender does not “offer” it as Mender has no idea on what the module could be doing in its install state.

For example, if your module installs xxx.txt, abc.py, def.py, the rollback state could remove xxx.txt, abc.py, def.py.

[Next page](https://hub.mender.io/t/script/328.md?page=2)
