Use of executeCommandLine correctly


I tried to start a pythonscript into the background fter my system is started:

rule "exec mqtt gpio handler"
    System started
        try {
                logInfo("execute /etc/openhab/scripts/ &", "start")
                var ScriptResponse = executeCommandLine(Duration.ofSeconds(60), "/etc/openhab/scripts/ &")
                logInfo("execute /etc/openhab/scripts/", "Start " + ScriptResponse.toString() )
        catch(Throwable T)
                logError("Error", "Some bad stuff happened in \"execute\": " + T.toString)



But the error is

2022-08-26 18:21:10.724 [WARN ] [] - Failed to execute commandLine '[/etc/openhab/scripts/ &]'

Any help, appreciated :slight_smile:

I see several things here and they all revolve around the &.

  1. When running a script from OH, you don’t get a shell. You don’t get stuff like pipes, IO redirection, nor can you run something in the background using &. That’s all stuff implemented by the shell.

  2. Even if running something in the background were possible, when you run executeCommandLine with a duration, it will wait for the script to exit before it returns with a return code. I don’t think you get a return code when you run something in the background, or if you do it won’t be 0 meaning success, so it will think that the script failed to run.

  3. When executeCommandLine runs a script, it creates a context in which that script runs. As soon as the script returns, that context is destroyed. Even if it were possible to run the script in the background like this, as soon as that command returns the context will be destroyed meaning your running script would be destroyed too.

Based on the name of the script, I’m thinking this is a service that is supposed to stay running and listening for MQTT messages. If that’s the case, you need to use systemd and a .service file (lots of tutorials on that, here’s one I picked at random Run Shell Script as Systemd Service in Linux).

If this isn’t a long running script, remove the & from the command.