Command line command is not executed

I want to add a record to a csv file each time when my item is updated.
I have a rule with action:

...
then
	val String ms_val = String_MS_raw.state.toString

	executeCommandLine(String::format("echo \"%2$s;%1$s\" >> /media/data/big_storage/Temp/meteostation.csv", ms_val, now))

In log it says

17:47:26.759 [DEBUG] [AbstractDatagramChannelBinding:1389 ] - Received meteostation;1116;273;23;36 on the listener port from /0.0.0.0:4321
17:47:26.759 [DEBUG] [i.s.RegExTransformationService:42 ] - about to transform ‘meteostation;1116;273;23;36’ by the function 'meteostation;(.*)'
17:47:26.760 [DEBUG] [t.protocol.internal.UDPBinding:276 ] - transformed response is '1116;273;23;36’
17:47:26.760 [DEBUG] [m.r.internal.engine.RuleEngine:305 ] - Executing rule 'Parse raw meteostation’
17:47:26.760 [INFO ] [runtime.busevents :26 ] - String_MS_raw state updated to 1116;273;23;36
> 17:47:26.762 [INFO ] [g.openhab.io.net.exec.ExecUtil:64 ] - executed commandLine 'echo “2016-02-29T17:47:26.761+02:00;1116;273;23;36” >> /media/data/big_storage/Temp/meteostation.csv’
17:47:26.764 [INFO ] [runtime.busevents :26 ] - MS_Temperature state updated to 23
17:47:26.765 [INFO ] [runtime.busevents :26 ] - MS_Humidity state updated to 36
17:47:26.766 [INFO ] [runtime.busevents :26 ] - MS_CO2_Raw state updated to 273

However actually the line is not added to file as expected. If I run the command separatelly in command line - line is adding.
My system is ubuntu 14.04

Two possibilities I can think of:

  1. The user under which the openHAB server is running does not have permission to write to the file /media/data/big_storage/Temp/meteostation.csv, whereas the user under which you are logged in at a command prompt does have permission.
  2. You don’t have access to shell syntax like >>, in which case you could write and invoke a two-line Bash script instead:
#!/bin/bash
echo "$2;$1" >> /media/data/big_storage/Temp/meteostation.csv

so your rule would change to something like

executeCommandLine("/home/openhab/bin/append.sh" + ms_val + " " + now.toString)

remember to set the executable bit and ownership such that the user under which the openHAB server is running can run the script, for example:

sudo chown openhab:openhab /home/openhab/bin/append.sh
sudo chmod +x /home/openhab/bin/append.sh
1 Like

Thanks
This works fine for me. Just checked. Slightly modified solution

Bash script

#!/bin/bash
echo $1 >> $2

rule code

val String ms_val = String_MS_raw.state.toString
executeCommandLine(String::format("/media/data/big_storage/Projects/electronics/co2/append.sh %2$s;%1$s /media/data/big_storage/Temp/meteostation.csv", ms_val, now))
1 Like

May I ask why do you not use the logging action or the logging persistence?

Does it allow to store files in csv format?
If not I think it would be nice to have additionally textual persistence (which would allow to save csv as well)

CSV is a separated formatted text. So only the suffix is another one.
I think if you define your csv file in the logback.xml and use the logging-action (logInfo, …) within your rule, you should be fine!

Yes, the logging persistence can be configured to save your items as CSV. See the Logging Persistence wiki page for details.

I highly recommend using the version of executeCommandLine that returns the results and logging those results. I find the Exec binding and executeCommandLine to be pretty brittle and it saves tons of time to actually have the results from executing a command already in your logs.

val results = executeCommandLine("/home/openhab/bin/append.sh" + ms_val + " " + now.toString, 5000)
logInfo("Results", results)

This doesn’t help now that you have it working but will help you when it suddenly stops someday.