Executing a Script

Hello Community,

I’m trying to run a script (script.sh) that will add, to an already created text file, a line with the current date. This script should be executed every time a switch item changes state. To do that I created a rule. I already gave all users the right to execute the script with chmod, and i can execute the script from a shell with the following command:
sudo -u openhab script.sh

rights:
image

script.sh:

#!/bin/bash

current_time=$(date "+%Y.%m.%d-%H.%M.%S")

echo $current_time >> t.txt

rules:

var results

rule "Create Timestamp"
when
  Item FF_CF_CONTROL changed
then 
  if (FF_CF_CONTROL.state == ON)
  	results = executeCommandLine("/home/bob/Desktop/script.sh")
  else 
  	results = executeCommandLine("/home/bob/Desktop/script.sh")
  logInfo("test", "exec result " + results)
end

I am getting this error when i change the state of the item “FF_CF_CONTROL”:

17:11:00.512 [WARN ] [org.openhab.core.io.net.exec.ExecUtil] - Error occurred when executing commandLine '[/home/bob/Desktop/script.sh]'
java.io.IOException: Cannot run program "/home/bob/Desktop/script.sh": error=13, Permission denied
	at java.lang.ProcessBuilder.start(ProcessBuilder.java:1128) ~[?:?]
	at java.lang.ProcessBuilder.start(ProcessBuilder.java:1071) ~[?:?]
	at org.openhab.core.io.net.exec.ExecUtil.executeCommandLine(ExecUtil.java:59) ~[?:?]
	at org.openhab.core.model.script.actions.Exec.executeCommandLine(Exec.java:40) ~[?:?]
	at jdk.internal.reflect.GeneratedMethodAccessor76.invoke(Unknown Source) ~[?:?]
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
	at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:1192) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:1167) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._invokeFeature(XbaseInterpreter.java:1153) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeFeature(XbaseInterpreter.java:1098) ~[?:?]
	at org.openhab.core.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:151) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:1008) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:971) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:247) ~[?:?]
	at org.openhab.core.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:227) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:1318) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:239) ~[?:?]
	at org.openhab.core.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:227) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:488) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:267) ~[?:?]
	at org.openhab.core.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:227) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:475) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:251) ~[?:?]
	at org.openhab.core.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:227) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:213) ~[?:?]
	at org.openhab.core.model.script.runtime.internal.engine.ScriptImpl.execute(ScriptImpl.java:80) ~[?:?]
	at org.openhab.core.model.script.runtime.internal.engine.DSLScriptEngine.eval(DSLScriptEngine.java:131) ~[?:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.lambda$0(ScriptActionHandler.java:62) ~[?:?]
	at java.util.Optional.ifPresent(Optional.java:183) [?:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.execute(ScriptActionHandler.java:59) [bundleFile:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1180) [bundleFile:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.runRule(RuleEngineImpl.java:988) [bundleFile:?]
	at org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData.run(TriggerHandlerCallbackImpl.java:89) [bundleFile:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
	at java.lang.Thread.run(Thread.java:829) [?:?]
Caused by: java.io.IOException: error=13, Permission denied
	at java.lang.ProcessImpl.forkAndExec(Native Method) ~[?:?]
	at java.lang.ProcessImpl.<init>(ProcessImpl.java:340) ~[?:?]
	at java.lang.ProcessImpl.start(ProcessImpl.java:271) ~[?:?]
	at java.lang.ProcessBuilder.start(ProcessBuilder.java:1107) ~[?:?]
	... 43 more
17:11:00.598 [INFO ] [org.openhab.core.model.script.test   ] - exec result null

Any clue?

Thank you.

Bernardo

In OH3 I think you need the timeout parameter first… like this:

val String response = executeCommandLine(Duration.ofSeconds(30),srcdir + "scripts/hp.sh")

Hello Martin,

I changed as you said, does the “hp” mean something?

var results

rule "Create Timestamp"
when
  Item FF_CF_CONTROL changed
then 
  if (FF_CF_CONTROL.state == ON)
  	results = executeCommandLine(Duration.ofSeconds(30),"/home/bob/Desktop/script.sh")
  else 
  	results = executeCommandLine(Duration.ofSeconds(30),"/home/bob/Desktop/script.sh")
  logInfo("test", "exec result " + results) 
end

Still get this error:

18:18:56.116 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'FF_CF_CONTROL' changed from ON to OFF
18:18:56.425 [WARN ] [org.openhab.core.io.net.exec.ExecUtil] - Failed to execute commandLine '[/home/bob/Desktop/script.sh]'
18:18:56.445 [INFO ] [org.openhab.core.model.script.test   ] - exec result null

No that’s just what I use to identify the script to be run. So in my example I’m just running the script

/etc/openhab/scripts/hp.sh

My file permissions are similar to yours:

-rwxrwxr-x 1 openhab    openhab        hp.sh

I’m assuming user ‘openhab’ has access to the folder /home/bob/Desktop ? (I’ve learned never to assume anything…!) It might also be worth explicitly specifying the path name of t.txt in your script, in case that’s not writing to where you expect it to…

echo $current_time >> /home/bob/Desktop/t.txt
1 Like