executeCommandLine script does not write the file

Environment:

Linux openhabian 6.6.62+rpt-rpi-v7 #1 SMP Raspbian 1:6.6.62-1+rpt1 (2024-11-25) armv7l GNU/Linux
OH 4.3.2

I’m testing executeCommandLine using a simple bash script and looks like the script cannot write to a file when called by OH Rule.

Tested via command line and it works:

CMD:

[14:59:51] root@openhabian:/etc/openhab/scripts# sudo -u openhab /etc/openhab/scripts/t.sh
OK

OH log:

==> /var/log/openhab/openhab.log <==

2025-01-22 15:01:25.165 [INFO ] [g.openhab.core.model.script.Ext_bash] - OK

==> /var/log/openhab/events.log <==

Rule:

var ScriptResponse = executeCommandLine(Duration.ofSeconds(2), "/etc/openhab/scripts/t.sh");
logInfo("Ext_bash", ScriptResponse);

bash script:

#!/usr/bin/bash
#
PWD=$(pwd)
#
FILEOut="$PWD/fileout.txt"
#
#
echo BlaBla >> $FILEOut
#
#
echo OK
#
#
exit 0

Can anyone show me what I’m doing wrong?

I assume the file has been written. Could you double check the location of the file by adding this to your script?

echo $PWD

/etc/openhab/scripts is not really intended for shell scripts. It’s original purpose is to store Rules DSL scripts. You can put shell scripts there but you’ll have to always use the full path.

Every user has a home folder and the openhab user is no different.

When OH executes a command or shell, it started in it’s home directory.

For an apt/yum installed OH the home directory is /var/lib/openhab.

Since you are using what ever directory the spell accept is starting in, I’m going to guess your script is working the file, you are just looking in the wrong place. It’s almost certainly /var/lib/openhab/fileout.txt

1 Like

Here is the script changed:

#!/usr/bin/bash
#
PWD=$(pwd)
#
echo $PWD
#
FILEOut="$PWD/fileout.txt"
#
#
echo BlaBla >> $FILEOut
#
#
echo OK
#
#
exit 0

and output

[17:30:45] root@openhabian:/etc/openhab/scripts# sudo -u openhab /etc/openhab/scripts/t.sh
/etc/openhab/scripts
OK

You’re right!

The file is at /var/lib/openhab.

When script is running it uses current user environment.

I must set the output path in the bash script.

About scripts folder I probably misunderstand the docs.

When I started this test I was using Blockly.

Function: Calls a script file with the given name which must be located in the $OPENHAB_CONF/scripts/ folder.

right, and when you follow the link in that section of the docs you go to Actions | openHAB and down to “callScript” it further says

Scripts are small pieces of Rules DSL code that can be called from Rules…

You cannot use the “call script” block to invoke an arbitrary shell script. That’s why we have a separate executeCommandLine action.

Thank you for your help.

It was a silly mistake (my bad) not setting the path inside the bash script.

You did.
If I run the script, echo $PWD results in
/var/lib/openhab

I don‘t know why it shows /etc/openhab/scripts on your device while it saves to /var/lib/openhab

The shell script is located in /etc/openhab/scripts. But when the script is run it’s run from $home, i.e ~openhab, i.e. /var/lib/openhab. When the script starts it gets pwd which is the folder the process is in, not the folder where the shell script file itself is.

No, and yes, I know. What I wanted to say is that the different output is weird.
As expected the output of pwd on my device was /var/lib/openhab.
That’s why I requested the OP to add an echo $PWD to his script as I was confident that the file was created somewhere else.

However, the output of the OP’s script was /etc/openhab/scripts

Probably there is a cd /etc/openhab/scripts command somewhere before.

in that case the current path of the user was /etc/openhab/scripts when he executed sudo -u.
sudo -u does not change the path that is why it is /etc/openhab/scripts.

This is what happened.

The test was done using “/etc/openhab/scripts” path.

Thanks for everyone’s help.