Stop/Start OH2 from Sitemap with Rule/Script

I’m working to have a button on a sitemap that will Stop OH2, then update and restart. I think I’m pretty close but may have a permission or some other issue. So far I have done the following.

  1. I created a sudoers file /etc/soduers.d/openhabcontrol and have this to the file

I then have this script to stop OH2, wait a few seconds and then restart it.

systemctl stop openhab2.service
sleep 30s
#apt-get -qq update
sleep 30s
systemctl start openhab2.service

And as part a rule when a switch is turned on, it runs this command

executeCommandLine("sudo /etc/openhab2/scripts/", 5000)

Now, if I run the script manually in a terminal, OH2 will stop and then start. But when I try this on the switch in a sitemap, OH2 will stop but it does not start again like it should.

Any ideas why it would not start again when running the script via openhab in a sitemap?

I tried something similar in the past. It seems that whatever thread starts that script terminates prematurely with the shutdown of OH therefore it never sends anything after OH itself is stopped.

For what it’s worth, you may want to narrow down that sudo config. You basically just made the OH user root and without a password.

Yes, I plan to tighten that up to just this script once I have it working. Just struggling to get it to restart. And seems strange running via a terminal, the script will stop and start it. But I can’t get OH to do the same when running it.

To elaborate on what Moxified said. When you use executeCommandLine you get a new shell environment running as the openhab user whose parent is the openHAB process.

What is happening is that the openHAB process is exiting before the script command completes. When the openHAB process exits all of the subprocesses are also killed which causes the script process to also be killed. Consequently, the script is being killed before it ever gets to the first sleep 30s.

tl;dr, you can’t run a script like this from openHAB.

quick and dirty: try to use screen, this will not drop the shell.

You could start a second bash script, independently from openHAB via the starting mechanism of the used operating system. this script could read from a named pipe.

The script startef by openHAB would write the „update“ command to the named pipe. Whenever the scond script receives this command it shuts down openhab by whicher mechanism is used by the OS, performes the update and starts openHAB again.

It might also be possible to implment a fork like behavior in bash, I am not sure about that. If that‘s not possible a small C Programm that is called by openHAB, forkes and calles the update Skript after that should work.

I would try the second bash script, reading from a named pipe, first, seems to be the easiest approch.

This a good idea, yet struggling to get it work.

I created 2 separate scripts. When the OH2 switch is turned ON, it is suppose to run this script to launch screen and then run the 2nd script to stop OH2, and then restart.

The first script is this"

screen -S oh2screen -d -m /etc/openhab2/scripts/

What’s also frustrating with the above is running screen -r always tells me no screens attached after running the script.

And then the upgrade script

systemctl stop openhab2.service
sleep 30s
sudo apt-get -qq update
sleep 30s
#sudo apt-get -qq upgrade openhab2
#sleep 120s
systemctl start openhab2.service

and in the rule

executeCommandLine("sudo /etc/openhab2/scripts/", 5000)

But again, seems I can run the first script which will then run the 2nd script and stop and start OH2. But when I try with OH2, it will stop OH2, but doesn’t seem to start again?

You’re running this with a 5 second timeout. Perhaps the timeout is being reached first, which is what’s killing the script?

Another thought would be to launch the upgrade script in the background from with a nohup.

So, your would look something like this (this might not be the correct syntax, but you get the idea).

nohup /path/to/ > /path/to/outputfile&

Then instrument the script so that you can see what it’s doing when you look at outputfile.

Keep in mind that screen sessions are only available to the user which started the session, so, if user openhab has started the session, you would have to use

sudo -u openhab screen -r

I’m using screen to start openHAB1 as a service. My command is:

sudo -u ${OHUSER} screen -S openHAB -dm  sh ./

so maybe (can’t remember…) you have to explicitly start the shell.