OpenHAB Exec runs every 30 seconds and also fails to execute commands

execbinding
exec
beginners
Tags: #<Tag:0x00007f1e5b9b1a88> #<Tag:0x00007f1e5b9b1948> #<Tag:0x00007f1e5b9b16f0>
(Ultratails42) #1

I could use some help. Ive been fiddling with trying to get OpenHAB2 working for the last few weeks on and off between just getting the linux distribution working and trying to figure out how OpenHAB2 works. Ive made some progress but am currently stuck at the exec function in two ways. One I may be able to figure out asI have found info related to it, but I can’t really try it yet until the other problem is fixed first. I am currently running openHAB 2.1.0 with the 2.1.0 exec binding.

About 99% of the things I want to run at the moment through OpenHAB will be done using the exec binding. Namely a SSH shutdown of a computer, as well as sending CEC signals through the pi to turn on my TV via libcec (As well as other various commands, most of which I have, though I am missing 1 or 2 that I can’t seem to find documentation on, but appear to be possible). I got libcec installed and functioning and have made scripts for each command as the formatting doesn’t play nice with the exec binding. I also have a functioning SSH command and publickey setup so the shutdown command can be sent with a single line without the need for the user to type in a password or such.

So I shall start with problem number 1. At the moment I have the things and items setup and are linked to the sitemap. I will include the code snippets below. The problem is at current, the exec commands run every 30 seconds regardless of whether the user has touched them. I am unsure as to why this is the case. I have only found one other user in the forum with this issue, and they found a workaround, but i’d rather find out if possible WHY this is happening to begin with and fix the issue directly. Below are the code snippers for my things and items. Most are currently commented out on the TV section as I have yet to write the scripts for them and was only testing with the TV On and SSH Shutdown commands.

Things

//This is the Things file

//HDTV
//Thing exec:command:TVON [command="echo "on" 0.0.0.0 | cec-client -s -d 1"]
//Thing exec:command:TVOFF [command="echo "standby" 0.0.0.0 | cec-client -s -d 1"]
//Thing exec:command:HDMI1 [command="echo "tx 4F:82:10:00" | cec-client -s -d 1"]
//Thing exec:command:HDMI2 [command="echo "tx 4F:82:20:00" | cec-client -s -d 1"]
//Thing exec:command:HDMI3 [command="echo "tx 4F:82:30:00" | cec-client -s -d 1"]
//Thing exec:command:HDMI4 [command="echo "tx 4F:82:40:00" | cec-client -s -d 1"]
//Thing exec:command:VOLUP [command="echo "tx 4F:82:10:00" | cec-client -s -d 1"]
//Thing exec:command:VOLDOWN [command="echo "voldown" | cec-client -s -d 1"]
Thing exec:command:TVON [command="/Scripts/TVON.sh", timeout=5, autorun=false]

//SSH Command
Thing exec:command:SHUTDOWN [command="ssh -p 2222 -l admin -i /home/pi/.ssh/PrivateKey.ppk 192.168.2.102 shutdown.exe /s /t 0", timeout=5, autorun=false]

Items

//This is the Items File
Switch MediaPC "Media-PC" (Status, Network) { wol="192.168.2.255#BC-AE-C5-E3-40-0C" }

//HDTV
Switch TVON "TV On" { channel="exec:command:TVON:run" }

//SSH Shutdown
Switch SHUTDOWN "Shutdown Media-PC" { channel="exec:command:SHUTDOWN:run" }

//Demo items
Switch DEMOSW "Demo Switch"

I will also include the output from Karaf

18:13:00.938 [INFO ] [marthome.event.ItemStateChangedEvent] - TVON changed from OFF to ON
18:13:01.132 [INFO ] [marthome.event.ItemStateChangedEvent] - TVON changed from ON to OFF
18:14:01.135 [INFO ] [marthome.event.ItemStateChangedEvent] - TVON changed from OFF to ON
18:14:01.331 [INFO ] [marthome.event.ItemStateChangedEvent] - TVON changed from ON to OFF
18:15:01.370 [INFO ] [marthome.event.ItemStateChangedEvent] - TVON changed from OFF to ON
18:15:01.583 [INFO ] [marthome.event.ItemStateChangedEvent] - TVON changed from ON to OFF
18:16:01.630 [INFO ] [marthome.event.ItemStateChangedEvent] - TVON changed from OFF to ON
18:16:01.778 [INFO ] [marthome.event.ItemStateChangedEvent] - TVON changed from ON to OFF
18:17:01.875 [INFO ] [marthome.event.ItemStateChangedEvent] - TVON changed from OFF to ON
18:17:02.091 [INFO ] [marthome.event.ItemStateChangedEvent] - TVON changed from ON to OFF

If anything else is needed, please let me know.

As for issue 2, aside from the fact the scripts run every 30 seconds, they actually do not work either. I have gotten them to a point where theres at least no more errors being spat out in the log files when I run them. With a friends help I was able to at least output a log file on the command being run which returns the error “failed to open vchiq instance”. I have found some info on this issue, so I might already have a fix for this in mind, but i’d like to hear what anyone has to say about this issue. I don’t plan to fix it until the constant script running issue is fixed first since it would cause my TV to turn on constantly if it’s fixed first.

As for my particular installation if thats important, I started from a clean Raspbian image (Running on a Raspberry Pi Zero W) and use the apt based install and have also made extensive documentation on the process since I had to redo this install a ton of times at first. So if any of that would be useful in solving the issues, I can provide that as well. I have a limited knowledge of linux however so keep that in mind (To note I am not a total noob, but I am primarily a high lever windows user and touch linux maybe once every few years if that. I do have friends who are very adept in linux though who have helped me through various issues in the setup. So please bare with me as I may or may not know what your speaking of if you get technical.).

0 Likes

(Rich Koshak) #2

First I’d like to say this is what a posting asking for help should look like. You explain the problem, you explain what you want to achieve, and you post all the relevant Things and Items. You even used code fences. :slight_smile: The only thing you neglected to post is what version of OH are you running?

Your first issue is a mystery to me. Without the interval set and with autorun set to false it should only be running when you issue a command to the Switch to run. It is a mystery to me. I run in Docker so the Exec binding is pretty useless to me so I can’t say I’ve seen anything like that before.

Based on the logs it looks like it is actually running every 60 seconds.

Do you have any Rules that do anything at all with these Items?

For Issue 2 the problem is almost certainly because your scripts are executing as the openhab user and the openhab user doesn’t have permission or is not configured with the right certs and such to ssh or open the files the script tries to open. See How to solve Exec binding problems for details about looking into that.

0 Likes

(Ultratails42) #3

Thank you. Ive always been the type to be quite clear on the help needed and such when asking as any and all information can be relevant. As for version, I am using OpenHAB 2.1.0 with the exec 2.1.0 version of the script (There was a massive change in how the script works between 1.0 and 2.0. Maybe I should try the legacy version which seems easier to use).

It too also confuses me as to why it keeps running. With timeout and autorun disabled, it really shouldn’t. Before this I did set a timer during troubleshooting to see if maybe setting a timer would cause it to no longer constantly go off, but that created a different odd problem. For example I had the timer set to 15 seconds, but it would instead execute every 7 seconds. Someone over here was having the exact same issue as seen here. He had a workaround by setting up rules, though i’ll be honest, I have no clue at the moment how that works, which should also answer the question about rules. Namely I don’t have any at the moment.

As for issue 2, this is what I figured. I have actually seen that post as well as others related, so I am confident I could fix that. But until issue 1 is solved I feel that would be dangerous at the moment since i’d then have the commands executing every minute.

0 Likes

(Ultratails42) #4

I may have actually solved issue 1. Was thinking again about the interval option again (Which as per the exec page entry lists it as “an interval, in seconds, the command will be repeatedly executed,”) and decided to re-add it and set it to zero. So far it hasn’t run by itself after about 40 minutes. Going to leave it on longer and see what happens. If that works I will move on to issue 2.

0 Likes

(Rich Koshak) #5

Interesting. In that case it would seem that the default interval is set for 30 or 60 seconds. This may be changed in the 2.2 SNAPSHOT but in case it isn’t, once you are satisfied that setting the interval to 0, it would be a good thing to file an issue on the openhab2-addons repo to either change the behavior so the default interval is 0 or to clarify this behavior in the docs.

0 Likes

(Ultratails42) #6

Agree’d. I have moved on to issue 2 which unfortunately is now baffling me and even friends of mine who have used linux there entire lives and run linux servers for businesses. Trying to fix this now. I did fix the initial issue of the “failed to open vchiq instance” by giving the openhab account proper permissions and giving it NOPASSWORD sudo access. Unfortunately all but one of my scripts fail to run. Only the TV On script works. All others now give a access denied error in Karaf. Changing the file and folder permissions of the scripts to make them all identical has not helped. In fact changing ANYTHING about them, even changing the user and group permissions to the openhab account or pi account causes openhab to no longer even see the files as existing.

0 Likes

(Rich Koshak) #7

Well, my first suggestion with Exec binding problems is to create a Design Pattern: Proxy Item that triggers a rule. Then use

val results = executeCommandLine("/full/path/to/script.sh argument", 1000)
logInfo("Testing", results)

This will tell you exactly what the script is returning and perhaps this will give you a clue as to what the problem is.

One other thing to try is to replace the spaces with @@.

"/full/path/to/script.sh@@argument"

Try executing the scripts as the openhab user from the command prompt:

sudo -u openhab /full/path/to/script.sh argument
0 Likes

(Ultratails42) #8

Took me a while, but I figured out the rules thing after some googling. It however spits out exactly the same error it did before. All I know is that the remaining issue at this time has to do with permissions, but I am unsure HOW. I have given the openhab account full permissions identical to the pi account, added to the NOPASSWORD sudo list (Even though these commands do not necessarily require sudo). But only one of them works. I did find out I can remove the " " lines from the script items and still have them work, so I had retried running the command directly instead of using a script, but this also did not work. I can run the commands with the “sudo -u openhab” command directly through terminal (Except SSH which complains about the keyfile permissions). I am also oddly only able to run the TVON.sh command through terminal as pi or openhab and no others. As an example of what I mean, I have included the terminal output for “ls -l” and me running the scripts with sudo (Same thing happens without).

pi@OpenHAB:/Scripts $ ls -l
total 36
-rwxr--r-- 1 root root 53 Nov 30 18:25 HDMI1.sh
-rwxr--r-- 1 root root 53 Nov 30 18:25 HDMI2.sh
-rwxr--r-- 1 root root 53 Nov 30 18:25 HDMI3.sh
-rwxr--r-- 1 root root 53 Nov 30 18:25 HDMI4.sh
-rwxr--r-- 1 root root 98 Nov 30 18:25 SSH.sh
-rwxr--r-- 1 root root 84 Nov 30 18:44 TVOFF.sh
-rwxr-xr-x 1 root root 76 Nov 29 15:21 TVON.sh 
-rwxr--r-- 1 root root 46 Nov 30 18:25 VOLDOWN.sh
-rwxr--r-- 1 root root 44 Nov 30 18:25 VOLUP.sh 
pi@OpenHAB:/Scripts $ sudo /Scripts/TVON.sh   
pi@OpenHAB:/Scripts $ sudo /Scripts/TVOFF.sh
sudo: unable to execute /Scripts/TVOFF.sh: No such file or directory
pi@OpenHAB:/Scripts $ sudo -u openhab sudo /Scripts/TVON.sh
pi@OpenHAB:/Scripts $ sudo -u openhab sudo /Scripts/TVOFF.sh
sudo: unable to execute /Scripts/TVOFF.sh: No such file or directory

pi@OpenHAB:/Scripts $

I have tried changing the permissions so all files have the same permissions as TVON.sh, and even tried changing there users:groups with no luck either. The only thing I have found that works, and I have no idea why, is to make a copy of the TVON.sh file, edit the contents with the desired script, then rename it to that. That for some reason will work.

0 Likes

(Rich Koshak) #9

Is the openhab user a member of the same groups as the pi user? Permissions on a lot of resources, particularly hardware devices, is often controlled by group membership.

Only your TVON.sh script has execute permission for anyone besides root. All the rest of your script can only be executed by root.

sudo chmod a+x *.sh

This will give execute permission on all the scripts which should allow the openhab and pi users permission to execute them.

The first column indicates the persmissions on the file. The first letter indicates whether it is a directory or not (it will be d for directory and - for file). The next three letters indicate the permissions for the owner of the file, the second set of three letters indicate the permissions for the group and the third set of letters indicate the permissions for everyone else.

Each set of three letters are rwx which stand for “read”, “write”, “execute”.

Do that again and verify that there is an x in all three places in the permissions string.

0 Likes

(Ultratails42) #10

So the ls -l output I had included was actually the current (I had a version before where I changed everyone to full read, write, and execute and still only the TVON one worked.). Also yes. I gave the openhab account identical rights as the pi account. I did however just make a breakthrough and have figured out the issue (But not the cause). I realized TVON.sh I created directly through nano on the pi itself and had given the execute rights. This one worked. All the others though I made initially on my windows machine just because it was quicker, then moved them over via a samba share on the system and then copied those to the scripts folder and gave them the same permissions as TVON.

For whatever reason though, they didn’t work and in most cases, the system wouldn’t even see the file I was trying to execute, as can be seen in the above posts output. So I went and re-created every script via nano, and then gave them the same rights as TVON. Now every script except SSH works (SSH returns a Host key verification failed when run from the openhab account, but not pi. A friend helped me set this up though and can likely help me again, but are unavailable until Tuesday next week.).

So at current it seems most issues are solved. At least the ones I have run into. There is still stuff I haven’t even done yet like setting up the amazon alexa voice controls and kodi and plex controls, but those might not be too bad as those seem more straight forward (I hope). If I have any other questions though I will ask here. Your help has been very much appreciated in helping me get this set up. Hopefully no other issues rear there ugly head. Time will tell. Theres no more devices to add for the time being (Though I eventually plan to get smart light switches for several rooms in the house, but this is something that would require more research, permission, and possibly most importantly, money.).

Edit: Funny enough just after I posted this, I remembered the sudo -u command you mentioned earlier and added that to my ssh script to run it on the pi account, and that totally fixed the SSH issue. So thats fixed now too.

0 Likes

(Rich Koshak) #11

If you use Notepad then your scripts were riddled with ^M characters which Linux cannot handle.

In the future, I recommend using Notepad++ rather than Notepad, or if you do use Notepad, you should run dos2unix on the files once you copy them to your Linux machine which will strip off the ^M characters.

It sees the file but it can’t recognize that it is a script because there is no /bin/bash^M program to run the script with.

If you copy the contents of your ~pi/.ssh to ~openhab/.ssh then the openhab user will have the same ssh certs as the pi user and you won’t have to sudo -u pi to make it work.

Use the following commands to do the copy:

sudo cp -R ~pi/.ssh ~openhab/.ssh
sudo chown -R openhab:openhab ~openhab/.ssh
sudo chmod 600 ~openhab/.ssh/*

*NOTE: Given the loose settings you have provided by adding openhab to sudo and not requiring it to enter a password I’m assuming you are not worried about the security implications of having your pi and openhab user share the same ssh cert.

0 Likes

(Ultratails42) #12

I actually did use notepad++ to make the files. I use it for all my more advanced stuff like this. At least it’s working now. As for the commands listed below, they don’t seem to work correctly. The first 2 run without error, but the 3rd always returns with

pi@OpenHAB:/ $ sudo chmod 600 ~openhab/.ssh/*
chmod: cannot access '/var/lib/openhab2/.ssh/*': No such file or directory

Also no. I am not too worried about sharing. Besides. It’s the only way I have found to do a successful remote shutdown of the system. The only other method I have seen never worked, but I was already using a SSH server for remote shutdown before this originally when I used to use a amazon dash button for this purpose (Though the script used for that has long since stopped working for some unknown reason. Not that it matters anymore.).

0 Likes

(Ultratails42) #13

I have actually made more significant progress now and have voice control via alexa working as well as a few rules for controlling my TV and Computer. I only have a few things left now. Some of which should be easy like the Kodi bindings. I can’t seem to get my TP-Link HS100 to work though. There is a binding for it via the eclipse marketplace, But I suspect that may be a issue with a firmware update maybe, or maybe something else. That at least works natively with alexa, so it’s not a huge concern for now.

The only other issue is I had to use a work around for the wake on lan binding in my rules files as it seemed to cause a error whenever called as seen below

12:17:11.441 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'MediaPC On': An error occurred during the script execution: Couldn't invoke 'assignValueTo' for feature JvmVoid:  (eProxyURI: home.rules#|::0.2.0.2.0.0.0.0::0::/1)

However I just installed etherwake and added a wake on lan command to the script file that was also called for the rule, and that fixed the issue there.

0 Likes