# Restart/Shutdown openHAB 3 using ECMAScript 2021

**URL:** <https://community.openhab.org/t/restart-shutdown-openhab-3-using-ecmascript-2021/136005>\
**Category:** Scripts & Rules\
**Created:** [May 18, 2022, 2:57pm UTC](https://community.openhab.org/t/restart-shutdown-openhab-3-using-ecmascript-2021/136005 "2022-05-18T14:57:09Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeevs](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@Jeevs](https://community.openhab.org/u/Jeevs)\
**Post date:** [May 18, 2022, 2:57pm UTC](https://community.openhab.org/t/restart-shutdown-openhab-3-using-ecmascript-2021/136005/1 "2022-05-18T14:57:09Z")

</div>

- Platform information:
  - Hardware: Raspberry Pi 3 B
  - OS: openHABian
  - openHAB version:3.3.0 M5

[Beginner’s guide to an openHABian reboot/shutdown switch - Tutorials & Examples / Solutions - openHAB Community](https://community.openhab.org/t/beginners-guide-to-an-openhabian-reboot-shutdown-switch/71903)

Hi all,

I’ve implemented the reboot/shutdown logic mentioned in the above guide. I’m able to successfully restart openHAB with the openhab user via SSH.

However, I’m having difficulties restarting using rules.

Now, my logs show the following error for either reboot or shutdown rules (notice the last line):

Restart:

```auto
2022-05-18 15:55:43.138 [INFO] [utomation.script.file.maintenance.js] - {
  "oldState": "NULL",
  "newState": "Reboot",
  "state": "null",
  "receivedCommand": null,
  "receivedState": null,
  "receivedTrigger": null,
  "itemName": "openHABMaintenance_Action",
  "module": "c8152a90-7315-40f6-b562-bb1a8a07e648",
  "payload": {
    "type": "String",
    "value": "Reboot",
    "oldType": "String",
    "oldValue": "NULL"
  },
  "eventType": "change",
  "triggerType": "ItemStateChangeTrigger"
} "New State:" "Reboot"
2022-05-18 15:55:43.142 [INFO] [utomation.script.openhab.maintenance] - Rebooting openHAB
2022-05-18 15:55:43.150 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule 'openHABMaintenance': Fail to execute action: 1

```

Shutdown:

```auto
2022-05-18 15:56:21.488 [INFO] [utomation.script.file.maintenance.js] - {
  "oldState": "NULL",
  "newState": "PowerOff",
  "state": "null",
  "receivedCommand": null,
  "receivedState": null,
  "receivedTrigger": null,
  "itemName": "openHABMaintenance_Action",
  "module": "c8152a90-7315-40f6-b562-bb1a8a07e648",
  "payload": {
    "type": "String",
    "value": "PowerOff",
    "oldType": "String",
    "oldValue": "NULL"
  },
  "eventType": "change",
  "triggerType": "ItemStateChangeTrigger"
} "New State:" "PowerOff"
2022-05-18 15:56:21.493 [INFO] [utomation.script.openhab.maintenance] - Shutting Down openHAB
2022-05-18 15:56:21.501 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule 'openHABMaintenance': Fail to execute action: 1

```

The rule commands are:

```auto
executeCommandLine("sudo","reboot");
executeCommandLine("sudo","poweroff");

```

Any help would be greatly appreciated 🙂

Many thanks in advanced!

Jeevs 🙂

---

<div class="post-metadata">

**Author:** ![smhaller](https://community.openhab.org/user_avatar/community.openhab.org/smhaller/32/3218_2.png) [@smhaller](https://community.openhab.org/u/smhaller)\
**Post date:** [May 18, 2022, 4:19pm UTC](https://community.openhab.org/t/restart-shutdown-openhab-3-using-ecmascript-2021/136005/2 "2022-05-18T16:19:54Z")

</div>

Hi Jeevs

> [@Jeevs](#):
>
> I’ve implemented the reboot/shutdown logic mentioned in the above guide. I’m able to successfully restart openHAB with the openhab user via SSH.

Please forgive me if I’m wrong. But for me a restart of openHAB is a different thing than reboot.  
Are you able to issue a “sudo reboot” under openhab user?  
And what is shown on the shell (i.e. session should get closed after a while)?

Stefan

---

<div class="post-metadata">

**Author:** ![rpwong](https://community.openhab.org/user_avatar/community.openhab.org/rpwong/32/36036_2.png) [@rpwong](https://community.openhab.org/u/rpwong)\
**Post date:** [May 18, 2022, 11:49pm UTC](https://community.openhab.org/t/restart-shutdown-openhab-3-using-ecmascript-2021/136005/3 "2022-05-18T23:49:13Z")

</div>

> [@Jeevs](#):
>
> ```auto
> executeCommandLine("sudo","reboot");
> executeCommandLine("sudo","poweroff");
> 
> ```

Have you tried using RulesDSL actions? The original rules in the tutorial are RulesDSL, and I don’t know if these commands work in ECMAScript (which I know nothing about). You didn’t mention ECMA when you messaged me directly, so it didn’t occur to me to suggest this.

---

<div class="post-metadata">

**Author:** ![Jeevs](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@Jeevs](https://community.openhab.org/u/Jeevs)\
**Post date:** [May 20, 2022, 8:43am UTC](https://community.openhab.org/t/restart-shutdown-openhab-3-using-ecmascript-2021/136005/4 "2022-05-20T08:43:25Z")

</div>

Hi Stefan,

If I use the following commands (based on the guide) in SSH openHAB does restart. Please forgive me if that is different from a reboot.

```auto
sudo -u openhab /bin/bash
sudo shutdown -r

```

Thanks

---

<div class="post-metadata">

**Author:** ![Jeevs](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@Jeevs](https://community.openhab.org/u/Jeevs)\
**Post date:** [May 20, 2022, 8:46am UTC](https://community.openhab.org/t/restart-shutdown-openhab-3-using-ecmascript-2021/136005/5 "2022-05-20T08:46:34Z")

</div>

Apologies I thought I had mentioned it. Though, I’ve made a conscious decision to move away from RulesDSL. I’m happy to create a temporary RulesDSL file to ensure that this isn’t a permission error 🙂

---

<div class="post-metadata">

**Author:** ![smhaller](https://community.openhab.org/user_avatar/community.openhab.org/smhaller/32/3218_2.png) [@smhaller](https://community.openhab.org/u/smhaller)\
**Post date:** [May 21, 2022, 3:32pm UTC](https://community.openhab.org/t/restart-shutdown-openhab-3-using-ecmascript-2021/136005/6 "2022-05-21T15:32:55Z")

</div>

Hi Jeewan

Ok so that command works correctly.

The defined rules as we can see from the logs are triggered based on your activity.  
The missing point is now. How the Rule issues the command to reboot (i.e. the action).

I suspect it would look like

 ![reboot_seq](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/4/5/4565c8dd1e874437a6b5e551d980e73954b0900b.jpeg)

\*\* This is nothing real just an empty trial.  
As in both cases your rule reports “Fail to execute action: 1”, i suspect the error here. Please provide the details in order to figure it out.

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![Jeevs](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@Jeevs](https://community.openhab.org/u/Jeevs)\
**Post date:** [May 23, 2022, 1:12pm UTC](https://community.openhab.org/t/restart-shutdown-openhab-3-using-ecmascript-2021/136005/7 "2022-05-23T13:12:09Z")

</div>

Hi Stefan,

Thanks for this, so my complete rule is as follows:

```javascript
// OH Maintenance
rules.JSRule({
    id: "openHABMaintenance",
    name: "openHAB Maintenance",
    description: "A rule to either restart or shut down the openHAB service",
    triggers: [triggers.ItemStateChangeTrigger("openHABMaintenance_Action")],
    execute: event => {
        
        // Declare all variables
        const logger = log("openHAB.Maintenance");

        logger.debug("openHAB Maintenance - Start");
        console.info(event, "New State:", event.newState);

        if(event.newState == "Reboot") {
            logger.info("Rebooting openHAB");
            executeCommandLine("sudo", "shutdown", "-r", "now");
        } else if (event.newState == "PowerOff") {
            logger.info("Shutting Down openHAB");
            executeCommandLine("sudo","poweroff");
        }
        
        logger.debug("openHAB Maintenance - End");
    }
});

```

As always your help if greatly appreciated! 🙂

Many thanks,

Jeewan

---

<div class="post-metadata">

**Author:** ![smhaller](https://community.openhab.org/user_avatar/community.openhab.org/smhaller/32/3218_2.png) [@smhaller](https://community.openhab.org/u/smhaller)\
**Post date:** [May 23, 2022, 8:13pm UTC](https://community.openhab.org/t/restart-shutdown-openhab-3-using-ecmascript-2021/136005/8 "2022-05-23T20:13:38Z")

</div>

Jeewan

I’m not 100% sure, but I think the: executeCommandLine itself is the result for the error. If you compare for instance here:

> [@openHAB 3 ECMA executeCommandLine](https://community.openhab.org/t/openhab-3-ecma-executecommandline/117267):
>
> Example how to executeCommandLine script in javascript, parse result and update items. var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.switcherStatus'); try{ var regStatus = /Device is (ON|OFF)/gm; var regCurrent = /Electric Current is: (\d+\.\d+)/gm; var regPowerConsumption = /Power consumption is: (\d{0,4})/gm; var regAutoShutdown = /Auto shutdown device in: (\d{0,2}:\d{0,2}:\d{0,2})/gm; var Exec = Java.type('org.openhab.core.model.script.actions.E…

In your script you have four “text strings” that you give.  
If i’m a dumb robot I connect this to: “sudoshutdown-rnow” and for sure I would reply, hmm I don’t understand.

Hope that helps 🙂

---

<div class="post-metadata">

**Author:** ![Jeevs](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@Jeevs](https://community.openhab.org/u/Jeevs)\
**Post date:** [May 24, 2022, 8:52am UTC](https://community.openhab.org/t/restart-shutdown-openhab-3-using-ecmascript-2021/136005/9 "2022-05-24T08:52:55Z")

</div>

Hi Stefan,

You are absolutely correct it is in fact the executeCommandLine that is the result of the error.

I’m essentially trying to get openHAB to restart/shutdown based on a item that I have created. This logic is all based on this guide: [Beginner’s guide to an openHABian reboot/shutdown switch - Tutorials & Examples / Solutions - openHAB Community](https://community.openhab.org/t/beginners-guide-to-an-openhabian-reboot-shutdown-switch/71903)

The only difference being that I’m attempting to use ECMAScript 2021 instead of RuleDSL…

Thanks,

---

<div class="post-metadata">

**Author:** ![Tougharms](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/4bbf92/32.png) [@Tougharms](https://community.openhab.org/u/Tougharms)\
**Post date:** [October 6, 2022, 1:37pm UTC](https://community.openhab.org/t/restart-shutdown-openhab-3-using-ecmascript-2021/136005/10 "2022-10-06T13:37:37Z")

</div>

@Jeevs

This works for me

```auto
actions.Exec.executeCommandLine(time.Duration.ofSeconds(1), 'sudo', 'reboot');

```

as does

```auto
let myAction = "sudo shutdown"
actions.Exec.executeCommandLine(time.Duration.ofSeconds(1), myAction);

```

---

<div class="post-metadata">

**Author:** ![Jeevs](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@Jeevs](https://community.openhab.org/u/Jeevs)\
**Post date:** [October 7, 2022, 11:10am UTC](https://community.openhab.org/t/restart-shutdown-openhab-3-using-ecmascript-2021/136005/11 "2022-10-07T11:10:35Z")

</div>

Works like an absolute charm - thanks so much @Tougharms!

As you can see was waiting for a resolution for a while 😃

Thanks once again for coming back and updating the thread 🙂 Very kind indeed!

---

<div class="post-metadata">

**Author:** ![Tougharms](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/4bbf92/32.png) [@Tougharms](https://community.openhab.org/u/Tougharms)\
**Post date:** [October 7, 2022, 11:31am UTC](https://community.openhab.org/t/restart-shutdown-openhab-3-using-ecmascript-2021/136005/12 "2022-10-07T11:31:40Z")

</div>

@Jeevs You are welcome. I hate it when threads are left hanging with no solution. It took me about 5 days to work this out, the online documentation and forum all mix old ECMA and ECMA2021.

Another piece of advice I found for using `actions.Exec.executeCommandLine`  
is to write your command in a short script eg “myscript.sh” in /home/openhabian then call the script with `actions.Exec.executeCommandLine(duration, "/home/openhabian/myscript.sh")`  
Dont forget to make the script executable!

---

<div class="post-metadata">

**Author:** ![Jeevs](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@Jeevs](https://community.openhab.org/u/Jeevs)\
**Post date:** [October 7, 2022, 2:26pm UTC](https://community.openhab.org/t/restart-shutdown-openhab-3-using-ecmascript-2021/136005/13 "2022-10-07T14:26:26Z")

</div>

> [@Tougharms](#):
>
> @Jeevs You are welcome. I hate it when threads are left hanging with no solution. It took me about 5 days to work this out, the online documentation and forum all mix old ECMA and ECMA2021

Could not agree more @Tougharms!

> [@Tougharms](#):
>
> Another piece of advice I found for using `actions.Exec.executeCommandLine`  
> is to write your command in a short script eg “myscript.sh” in /home/openhabian then call the script with `actions.Exec.executeCommandLine(duration, "/home/openhabian/myscript.sh)`  
> Dont forget to make the script executable!

Oh wow - that is certainly impressive, may well have to update the rule 😃

Thanks once again 🙂

p.s. already marked your previous post as the solution 🙂
