Run shell script by rule to stop openhab and more

Hi Wolfgang (@Wolfgang_S ) or any other linux expert,
sorry for pinging you - I remeber you as a linux expert and being very helpful in this forum.

For a couple of days I try to overcome the following problem:
From a rule I want to run a bash script which stops openhab, performs a few maintenance tasks and then starts openhab.

The problem is, when openhab has been stopped, the script stopped, too. Which is somehow explainable and logical. Anyway, I try to find a way that the script “survives” and continues as expected.

Let’s take this as our test.sh script:

#!/usr/bin/bash
systemctl stop openhab.service
echo "Whatever" >> test.txt
systemctl start openhab.service

which gets executed by

actions.Exec.executeCommandLine(time.Duration.ofSeconds(5),"sudo","/etc/openhab/scripts/test.sh");  

As said, after command 1, command 2+3 never gets executed (sure, test.sh is added to sudoers).

I tried to start the script in different approaches like

"sudo","test.sh","&"
"sudo","bash","-c","'test.sh'"
"exec,"sudo","test.sh"

but this did not work either.

Do you have an idea?

Plan b would be to create a systemd-timer but I want to pursue this further only if this approach definitely will not work.
During my test I also found out, that openhab starts a dash shell, not a bash shell, in case this makes a difference.

Try it without the duration argument

Thanks Jim.
Unfortunately it still does not work.

I found this while googling: Can Java execute batch file outside of current JVM in separate process tree - Oracle Forums

Unfortunately since I’m using Docker, I can’t test this, because stopping openhab will kill the docker (and everything in it, including that detached process).

Another approach that would work for my docker, would be to create a file in a mounted volume so it’s visible on the host. Then on the host, run a daemon file watch process, or simply a cron job every minute that checks for this file, and run whatever you need to run.

@rpwong has the definitive guide for this at Beginner's guide to an openHABian reboot/shutdown switch.

While it addressesd the whole machine, the way the script is run should apply here too I would think.

Interesting. Thanks Jim.
How can I run this java command in openhab - preferrably in javascript?

Runtime.getRuntime().exec("bash -c /etc/openhan/scripts/test.sh")

Meanwhile I pursued my plan b which was to make use of systemd-timers (similiar to your cron approach) but this one comes more handy:

actions.Exec.executeCommandLine(time.Duration.ofSeconds(5),"sudo","systemd-run","--on-active=10","/etc/openhab/scripts/test.sh");

This starts the script with a delay of 10 seconds as a new process and keeps running although openhab has been stopped. Now this scripot can perform some tasks and wil start openhab in the end.

Not sure if I get you right, Rich, but that guide on the first view is about how to reboot your device and how to set the required permissions so that you can execute commands which require higher permissions.
My problem is about how to run a script from openhab and making sure it continues although the script stops openhab service.

It’s the same problem there though because there you run a shell script which issues the shutdown command. That causes OH to be stopped which, under normal circumstances will kill the bash script same as is happening here, which cancels the shutdown.

That tutorial is thorough but I beleive it includes how to keep the bash script from being killed when OH goes down.

Hm. I read the guide but we are still not on the same page.

The difference is that there is no problem to power off or reboot your device or restart openhab service by systemctl command run by a rule. This single command gets executed allthough openhab service will be killed during this command.

The difference is in my scenario the rule starts a scripts where one of the commands is to stop openhab service and continue with other commands:

dummy bash script

command 1
systemctl stop openhab
command 2
command 3
systemctl start openhab

Here we are on the same page: systemctl stop openhab is being executed but that also kills the process of the “active” bash script as it is a child process of openhab and gets killed, too.
tldr, I still can’t see that this guide addresses this problem.

This sounds a bit related to Ensuring "CoAP Transport" is installed after openHAB upgrade.

I’m now away from home, but I notice my openHAB is now offline, after updating to 4.3.1.

I’m going to do some digging tomorrow, but I assume it keeps restarting openHAB before Coap is installed, failing the if clause after next restart etc. ad aeternam.

However, I think I changed the echo commands by mail commands, so I would expect my mailbox to get flooded… But no mails have arrived.

Erik, wrong thread?

I don’t think so :slight_smile: I gave a slightly different perspective, via a similar script (see link).

Sorry to say, no, it doesn‘t. Your script does not address the problem we are discussing here.
As soon as your script restarts openhab, it gets killed. In your case it is irrelevant because restart is the last command anyway.

Mmm, you’re right. Apologies, I mixed some things up…

Sorry I don’t know much about JavaScript.

You could do a delay of 0 or 1 second…

Can you use nohup to avoid this? In the past I believe it was also necessary to make sure stdout, stdin, and stderr were also closed, but I’m not sure about current usage.

Sorry, was afk for a few days.
As the problem is that the running shell will be killed and it looks like running the script with “&” does not seem to work either - based on your experiments I would do the following:
run/start a shell script that starts an at task:

#!/bin/sh
echo do something here if you like
echo /path/to/your/script | at now

This will immediately execute the content of your script.
As this hands over the execution to the at command script execution will continue to run.

You need to install the at package to make at jobs available.

I came across the at command too during my research but didn‘t pursue this further as I had to, as you wrote, install an additional package. I was hoping that there is a built-in solution of bash available like bash -c.

Finally I ended up with this command (same idea as yours):

systemd-run --on-active=2 /etc/openhab/scripts/test.sh

And thanks for your help.

1 Like