[SOLVED] Openhab 2 and executeCommandLine / Exec


I just switched from OH 1 to OH 2.and currently have the problem that executeCommandLine statements in rules aren’t executed like they were in OH 1.

I installed OH 2 from apt-repository (raspbian) and created all items, things, etc. new (no migration from OH 1).
The item simply looks like that:
Number light_hue_light_2_num

In the sitemap the item is used as following:
Selection item=light_hue_light_2_num label="hue 2 level" mappings=[0="OFF", 25="25%", 50="50%", 75="75%", 100="100%"]

The part of rule which should be executed is this:
val results = executeCommandLine("/etc/openhab2/scripts/hue_set_level.pl@@2@@50") logInfo("Exec", "Results from level to 50:\n" + results)

I can see that the rule is executed, but its result shown by logInfo is always null - even if a execute uptime e.g.

The user itself can run the script successfully.

Has the function of executeCommandLine been removed from OH 2 ?

Another approach would be to use the Exec-Binding. But do I understand correctly, that its purpose is to run system commands repeatedly and not on demand?

It would be great, if you could help me. Thanks and regards,

not sure, but I think Rich wrote somewhere, that you have to give the exec command some time to execute. So the command has to look something like this:

val results = executeCommandLine("/etc/openhab2/scripts/hue_set_level.pl@@2@@50",1000)

I use:

var String Antwort = executeCommandLine("/usr/scripts/tdrm_pollen.sh",10000)

and this is working in OH2

Thank you, Windrad.
I’ll try it the suggested way later. Knowing that executeCommandLine itself works in OH2 is already a big hint.

var String results = executeCommandLine("/etc/openhab2/scripts/hue_set_level.pl@@2@@50",1000)

This did the trick. Thank you, Windrad.

is executeCommandLine somehow deprecated?

Error during the execution of rule powermeter: An error occured during the script execution: The name ‘executeCommandLine(,)’ cannot be resolved to an item or type.

please show the exact line in the rule!

var String solar = executeCommandLine("/opt/openhab/configurations/scripts/pvstatus.sh", 5000)

this used to work and without even updating the OH2 installation I got the error in the log.

1 Like

hm that’s weird…
did you restart openhab? maybe something crashed during runtime?

I would not be surprised… the whole system seems to be unstable.

My current working theory is that the SD-card is going bananas…

I have the same problem, trying to emulate the hping & Arp presence detection for iPhone:

    var String IphoneExec = executeCommandLine("sudo@@bash@@/opt/iPhoneD.sh", 5000)


Error during the execution of rule Execute script IphonePresence: An error occurred during the script execution: The name 'executeCommandLine(<XStringLiteralImpl>,<XNumberLiteralImpl>)' cannot be resolved to an item or type.

Be interested if you find a solution.



I’ve the same issue:

 Rule 'Test': The name 'ExecuteCommandLine' cannot be resolved to an item or type; line 29, column 28, length 55

and this is the line:

var String result = ExecuteCommandLine("/etc/openhab2/test.sh", 60000)

How we can run external script from rules?


var String result = executeCommandLine("/etc/openhab2/test.sh", 60000)

No capital letter at execute

Just MHO

I wrestled with getting executeCommandLine and exec (v 1.1) to reliably work for a long time. Very frustrating in general.

Yesterday I switched to using the exec (v 2.2) binding. The setup of an execution command is a bit more indirect than the other two options (see the EXEC 2.2 binding doc) — BUT it only took two tries to “get it right” and the failed try was “pilot error code” on my part. Has been working beautifully (as in stable and reliable sense of “beauty”) for over 24 hours.

end MHO

I struggle with executeCommandLine. I’m pretty sure that the rule worked in the past. Can’t say if the upgrade to 2.3 broke it.

rule "HAB_Restart"
	Item HAB_Restart changed from OFF  to ON or
	Item HAB_Restart changed from NULL to ON
	logInfo("HAB_Restart", "openHAB is restarting!")
        executeCommandLine("systemctl@@restart@@openhab2", 60000)
	sendCommand(HAB_Restart, OFF)

I see “openHAB is restarting!” in the log, so the rule gets triggered, the item gets reset, but the command is not executed.

Any idea?

Try to get the response and post it to the log, maybe this will give more insight.

good hint:
Failed to restart openhab2.service: Interactive authentication required.
See system logs and ‘systemctl status openhab2.service’ for details.

Did you get it solved with that? Or is there a question left?

Hi All,

I have a backup script that executes, at the end of the script it prints a success.

Is there a way I can log this success (or fail) in the openhab log?

This is my current rule (backup works, but the logging doesnt)

rule "Backup OH2/Influx/Grafana"
 Time cron "0 0 1 ? * * *" or
 Item backupserver received command ON
 logInfo("Server Backup", "Backing up the Server")
 var String RCloneOutput = executeCommandLine("sudo /etc/openhab2/scripts/backup.sh", 60000)
 logInfo("Server Backup", "Backup Executed + RCloneOutput")

Due to the very long time out
I would put this in a timer:

rule "Backup OH2/Influx/Grafana"
    Time cron "0 0 1 ? * * *" or
    Item backupserver received command ON
    logInfo("Server Backup", "Backing up the Server")
    createTimer(now, [ |
        var String RCloneOutput = executeCommandLine("sudo /etc/openhab2/scripts/backup.sh", 60000)
        logInfo("Server Backup", "Backup Executed" + RCloneOutput)

That way you don’t block a thread for up to a minute

1 Like

So close I was ha!

I saw this in the log:

16:34:17.522 [INFO ] [.smarthome.model.script.Server Backup] - Backup Executedsudo: no tty present and no askpass program specified