# State scripting for Raspberry PI board running ubuntu 18.04

**URL:** <https://hub.mender.io/t/state-scripting-for-raspberry-pi-board-running-ubuntu-18-04/1203>\
**Category:** General Discussions\
**Tags:** state-scripts\
**Created:** [October 30, 2019, 5:38am UTC](https://hub.mender.io/t/state-scripting-for-raspberry-pi-board-running-ubuntu-18-04/1203 "2019-10-30T05:38:09Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![DipinPK](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/dipinpk/32/414_2.png) [@DipinPK](https://hub.mender.io/u/DipinPK)\
**Post date:** [October 30, 2019, 5:38am UTC](https://hub.mender.io/t/state-scripting-for-raspberry-pi-board-running-ubuntu-18-04/1203/1 "2019-10-30T05:38:09Z")

</div>

Hi All,

As a beginning, I successfully completed the single file update using mender in Raspberry PI board running with Ubuntu 18.04. Now I have another two things to do,

1. How to update a complete folder using mender ?
2. I would like to run a custom shell script as soon as the mender updated the new file. How to do that in mender using state scripting, I have read about the state scripting documentation and I have basic idea about the naming convention and the folder structure. But I would like to get the procedure to run a script here.

Any help will be completely appreciable. As I am new to mender, wish to be part of this.

---

<div class="post-metadata">

**Author:** ![DipinPK](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/dipinpk/32/414_2.png) [@DipinPK](https://hub.mender.io/u/DipinPK)\
**Post date:** [October 30, 2019, 10:12am UTC](https://hub.mender.io/t/state-scripting-for-raspberry-pi-board-running-ubuntu-18-04/1203/2 "2019-10-30T10:12:28Z")

</div>

Hi all,

As I am stuck in the state scripting part, expecting some help from the mender experts or people who already worked with mender.

Regards  
Dipin

---

<div class="post-metadata">

**Author:** ![kacf](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/kacf/32/146_2.png) [@kacf](https://hub.mender.io/u/kacf)\
**Post date:** [October 30, 2019, 12:25pm UTC](https://hub.mender.io/t/state-scripting-for-raspberry-pi-board-running-ubuntu-18-04/1203/3 "2019-10-30T12:25:47Z")

</div>

> How to update a complete folder using mender

You can use the [Directory Update Module](https://hub.mender.io/t/directory/325) to deploy directory updates. There are usage instructions in that link. Note that you have to put the update module in the `/usr/share/mender/modules/v3` directory on the device before you can use them.

> I would like to run a custom shell script as soon as the mender updated the new file. How to do that in mender using state scripting, I have read about the state scripting documentation and I have basic idea about the naming convention and the folder structure. But I would like to get the procedure to run a script here.

Just specify `-s ArtifactInstall_Leave_50_myscript` when you make the artifact, and this script will execute after the install stage. You can replace `myscript` with whatever you want, and the contents can be whatever you want. If you use the artifact generation script for the Directory Update Module, then you can specify such options at the end, after a `--`. So for example:

```auto
./directory-artifact-gen -n ${ARTIFACT_NAME} -t ${DEVICE_TYPE} -d ${DEST_DIR} -o ${OUTPUT_PATH} ${FILE_TREE} -- -s ArtifactInstall_Leave_50_myscript

```

---

<div class="post-metadata">

**Author:** ![DipinPK](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/dipinpk/32/414_2.png) [@DipinPK](https://hub.mender.io/u/DipinPK)\
**Post date:** [October 31, 2019, 6:24am UTC](https://hub.mender.io/t/state-scripting-for-raspberry-pi-board-running-ubuntu-18-04/1203/4 "2019-10-31T06:24:42Z")

</div>

Thanks for you help.

I am getting an error like _./directory-artifact-gen: line 32: hash: mender-artifact: not found_. But I downloaded the latest mender artifact binary and located in _/usr/bin/_ folder of my computer.

Please suggest to resolve this issue

---

<div class="post-metadata">

**Author:** ![kacf](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/kacf/32/146_2.png) [@kacf](https://hub.mender.io/u/kacf)\
**Post date:** [October 31, 2019, 7:27am UTC](https://hub.mender.io/t/state-scripting-for-raspberry-pi-board-running-ubuntu-18-04/1203/5 "2019-10-31T07:27:15Z")

</div>

Did you also make it executable?

```bash
chmod ugo+x /usr/bin/mender-artifact

```

---

<div class="post-metadata">

**Author:** ![DipinPK](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/dipinpk/32/414_2.png) [@DipinPK](https://hub.mender.io/u/DipinPK)\
**Post date:** [October 31, 2019, 8:39am UTC](https://hub.mender.io/t/state-scripting-for-raspberry-pi-board-running-ubuntu-18-04/1203/6 "2019-10-31T08:39:28Z")

</div>

Solved the issue, Thank you very much.  
Successfully updated directory also.

---

<div class="post-metadata">

**Author:** ![DipinPK](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/dipinpk/32/414_2.png) [@DipinPK](https://hub.mender.io/u/DipinPK)\
**Post date:** [October 31, 2019, 9:10am UTC](https://hub.mender.io/t/state-scripting-for-raspberry-pi-board-running-ubuntu-18-04/1203/7 "2019-10-31T09:10:47Z")

</div>

Tried to run a script as per your suggestion. I could able to sucesfully create an artifact with my custom script. But the update fails. Attached the screenshot of the failure. My script name is _ArtifactInstall\_Leave\_50\_myscript_

 ![Screenshot%20from%202019-10-31%2014-37-14](https://canada1.discourse-cdn.com/flex036/uploads/mender/original/1X/2d938ee272c9f5a1498dfcf5ffd5500fba6dc1e9.png).

Any modifications required in the client side to run this script?

---

<div class="post-metadata">

**Author:** ![kacf](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/kacf/32/146_2.png) [@kacf](https://hub.mender.io/u/kacf)\
**Post date:** [October 31, 2019, 9:30am UTC](https://hub.mender.io/t/state-scripting-for-raspberry-pi-board-running-ubuntu-18-04/1203/8 "2019-10-31T09:30:23Z")

</div>

What does the log say?

---

<div class="post-metadata">

**Author:** ![DipinPK](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/dipinpk/32/414_2.png) [@DipinPK](https://hub.mender.io/u/DipinPK)\
**Post date:** [October 31, 2019, 9:33am UTC](https://hub.mender.io/t/state-scripting-for-raspberry-pi-board-running-ubuntu-18-04/1203/9 "2019-10-31T09:33:36Z")

</div>

2019-10-31 09:01:41 +0000 UTC error: transient error: error executing leave script for update-install state: error running leave state script(s) for ArtifactInstall state: statescript: error executing ‘ArtifactInstall\_Leave\_50\_myscript’: -1 : fork/exec /var/lib/mender/scripts/ArtifactInstall\_Leave\_50\_myscript: exec format error  
2019-10-31 09:01:41 +0000 UTC info: State transition: update-install [ArtifactInstall] -\> rollback [ArtifactRollback].

This is the part of log which talks about the script.

---

<div class="post-metadata">

**Author:** ![DipinPK](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/dipinpk/32/414_2.png) [@DipinPK](https://hub.mender.io/u/DipinPK)\
**Post date:** [October 31, 2019, 10:48am UTC](https://hub.mender.io/t/state-scripting-for-raspberry-pi-board-running-ubuntu-18-04/1203/10 "2019-10-31T10:48:50Z")

</div>

It will be great help if anyone can help me to solve this issue.

---

<div class="post-metadata">

**Author:** ![kacf](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/kacf/32/146_2.png) [@kacf](https://hub.mender.io/u/kacf)\
**Post date:** [November 1, 2019, 6:57am UTC](https://hub.mender.io/t/state-scripting-for-raspberry-pi-board-running-ubuntu-18-04/1203/11 "2019-11-01T06:57:58Z")

</div>

Are you able to execute the script manually, if you copy it over to the device? Again you will need to make it executable if it isn’t.

What kind of script is it? Bash script? Maybe you can post it here, if it’s not sensitive.

---

<div class="post-metadata">

**Author:** ![DipinPK](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/dipinpk/32/414_2.png) [@DipinPK](https://hub.mender.io/u/DipinPK)\
**Post date:** [November 4, 2019, 4:20am UTC](https://hub.mender.io/t/state-scripting-for-raspberry-pi-board-running-ubuntu-18-04/1203/12 "2019-11-04T04:20:09Z")

</div>

I could able to execute the script manually from the server environment. If i make the artifact along with script, the new directory is not even updating in the device. Its an empty folder.  
The script is a small shell script. The content of the script is only _echo “Hello”_ for testing purpose.  
And while naming the script file it should be only _ArtifactInstall\_Leave\_50\_myscript_ or _ArtifactInstall\_Leave\_50\_myscript.sh_

---

<div class="post-metadata">

**Author:** ![kacf](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/kacf/32/146_2.png) [@kacf](https://hub.mender.io/u/kacf)\
**Post date:** [November 4, 2019, 7:18am UTC](https://hub.mender.io/t/state-scripting-for-raspberry-pi-board-running-ubuntu-18-04/1203/13 "2019-11-04T07:18:17Z")

</div>

A couple of things to check:

- Does the script contain the `#!/bin/sh` shebang at the top of the script? If is is `#!/bin/bash`, try changing it to `#!/bin/sh`
- Are any of your filesystems mounted with the `noexec` option? What is the output from `mount`?

The `.sh` extension makes no difference, the script should execute either way.

---

<div class="post-metadata">

**Author:** ![DipinPK](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/dipinpk/32/414_2.png) [@DipinPK](https://hub.mender.io/u/DipinPK)\
**Post date:** [November 4, 2019, 8:59am UTC](https://hub.mender.io/t/state-scripting-for-raspberry-pi-board-running-ubuntu-18-04/1203/14 "2019-11-04T08:59:49Z")

</div>

Thank you very much 😀  
After changing to `#!/bin/sh` i could able to deploy the artifact without any error. But how can I verify that my script is running on the device. I am not able to see my script on the device actually

---

<div class="post-metadata">

**Author:** ![kacf](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/kacf/32/146_2.png) [@kacf](https://hub.mender.io/u/kacf)\
**Post date:** [November 4, 2019, 9:23am UTC](https://hub.mender.io/t/state-scripting-for-raspberry-pi-board-running-ubuntu-18-04/1203/15 "2019-11-04T09:23:11Z")

</div>

You cannot see it directly, because the script only exists while the update is being installed. But if the script leaves a trace somewhere (for example `touch /tmp/script-was-here`), you can look for it afterwards.

---

<div class="post-metadata">

**Author:** ![DipinPK](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/dipinpk/32/414_2.png) [@DipinPK](https://hub.mender.io/u/DipinPK)\
**Post date:** [November 4, 2019, 9:31am UTC](https://hub.mender.io/t/state-scripting-for-raspberry-pi-board-running-ubuntu-18-04/1203/16 "2019-11-04T09:31:47Z")

</div>

Thank you for your help… I created a folder using script, so that I can make sure the script is working. Tested the same here and it worked well

---

<div class="post-metadata">

**Author:** ![SamanRatna](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@SamanRatna](https://hub.mender.io/u/SamanRatna)\
**Post date:** [August 10, 2020, 3:32pm UTC](https://hub.mender.io/t/state-scripting-for-raspberry-pi-board-running-ubuntu-18-04/1203/17 "2020-08-10T15:32:51Z")

</div>

./directory-artifact-gen -n {ARTIFACT\_NAME} -t {DEVICE\_TYPE} -d {DEST\_DIR} -o {OUTPUT\_PATH} ${FILE\_TREE} – -s ArtifactInstall\_Leave\_50\_myscript

This command gives: – -s command not found and without --, it gives unsupported option-s
