Start a new topic
Answered

Scripting : having the list of the running session with __ctx__.executeCommand

Is __ctx__.executeCommand still available?

I'm trying to set up a scipt to telle if my current process is already running using __ctx__.executeCommand like this : 


_ctx__.executeCommand(get sessions TEST_PROCESS_CONCURRENT status running)


The complet jython script is the following : 

import time

Process_To_Check = __ctx__.getVariableValue("${../CALLING_SESSION}$")

Iter_Max = __ctx__.getVariableValue("%{PITER}%")

Wait_Delay = __ctx__.getVariableValue("%{PWAITDELAY}%")

Iter = 1

while Iter < Iter_Max:

    found = 0

     s = __ctx__.executeCommand(get sessions Process_To_Check status running)

 running)

    lst = s.split("\n")

    for i in range(len(lst)):

        if lst[i].startswith("${../PROCESS_TO_CHECK}$" + " "):

            found += 1

    time.sleep(Wait_Delay)

    Iter += 1

if found > 1:

    raise Exception("Another process is already running")


image

But I have the following error : 

javax.script.ScriptException: SyntaxError: no viable alternative at input 'sessions' in <script> at line number 12 at column number 35

 at org.python.jsr223.PyScriptEngine.scriptException(PyScriptEngine.java:213)

 at org.python.jsr223.PyScriptEngine.compileScript(PyScriptEngine.java:93)

 at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:31)

 at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)

 at com.indy.engine.actionCodes.ScriptingActionCodeI._executeCodewithRSet(ScriptingActionCodeI.java:84)

 at com.indy.engine.actionCodes.ScriptingActionCodeI.executeCodewithRSet(ScriptingActionCodeI.java:152)

 at com.indy.engine.actionCodes.ScriptingActionCodeI.executeSimpleCode(ScriptingActionCodeI.java:229)

 at com.indy.engine.manager.ActionCodeExecutor.executeCode(ActionCodeExecutor.java:140)

 at com.indy.engine.manager.ActionCodeExecutor.run(ActionCodeExecutor.java:267)

 at java.base/java.lang.Thread.run(Thread.java:829)

Caused by: File "<script>", line 12

    s = __ctx__.executeCommand(get sessions TEST_PROCESS_CONCURRENT status running)

                                  ^

SyntaxError: no viable alternative at input 'sessions'


 

 

 

 


Best Answer

Hello,


Yes, __ctx__.executeCommand is still available and fully supported. Its signature is:


public String executeCommand(String command) {}


The key point is that it expects a String as its argument, i.e. the command must be passed between quotes. In your script, you're passing the command as raw, unquoted text:


__ctx__.executeCommand(get sessions TEST_PROCESS_CONCURRENT status running)


Jython (Python) tries to interpret "get", "sessions", etc. as Python identifiers/keywords rather than as a string, which is exactly what causes the SyntaxError: no viable alternative at input 'sessions' you're seeing. This has nothing to do with the method being deprecated, it's simply a Python string-quoting issue.


The fix is to wrap the command in quotes, and build it dynamically using string concatenation if you need to inject the Process_To_Check variable:


s = __ctx__.executeCommand("get sessions " + Process_To_Check + " status running")


Or, if you always want to check the same fixed process name:


s = __ctx__.executeCommand("get sessions TEST_PROCESS_CONCURRENT status running")


A couple of additional remarks while reviewing your script:


- The found variable is initialized to 0 inside the while loop, so its value from a previous iteration is lost. If you want to accumulate matches across iterations, initialize it before the loop instead.

- The final check (if found > 1) is outside the while loop, so it only evaluates the value of found from the very last iteration, not the overall result across all iterations.


Let us know if you'd like help adjusting the loop logic once the syntax issue is resolved.


Best regards

1 Comment

Answer

Hello,


Yes, __ctx__.executeCommand is still available and fully supported. Its signature is:


public String executeCommand(String command) {}


The key point is that it expects a String as its argument, i.e. the command must be passed between quotes. In your script, you're passing the command as raw, unquoted text:


__ctx__.executeCommand(get sessions TEST_PROCESS_CONCURRENT status running)


Jython (Python) tries to interpret "get", "sessions", etc. as Python identifiers/keywords rather than as a string, which is exactly what causes the SyntaxError: no viable alternative at input 'sessions' you're seeing. This has nothing to do with the method being deprecated, it's simply a Python string-quoting issue.


The fix is to wrap the command in quotes, and build it dynamically using string concatenation if you need to inject the Process_To_Check variable:


s = __ctx__.executeCommand("get sessions " + Process_To_Check + " status running")


Or, if you always want to check the same fixed process name:


s = __ctx__.executeCommand("get sessions TEST_PROCESS_CONCURRENT status running")


A couple of additional remarks while reviewing your script:


- The found variable is initialized to 0 inside the while loop, so its value from a previous iteration is lost. If you want to accumulate matches across iterations, initialize it before the loop instead.

- The final check (if found > 1) is outside the while loop, so it only evaluates the value of found from the very last iteration, not the overall result across all iterations.


Let us know if you'd like help adjusting the loop logic once the syntax issue is resolved.


Best regards

Login to post a comment