SysProcessCreate2 on Linux runtime

2025-03-16
2025-03-17
  • trusty-squire - 2025-03-16

    Hi,
    I'm trying to launch a continuously running python script from Codesys and monitor the script status inside the Codesys runtime (running on a Linux enviroment). For now I'm testing with the below to emulate a continuously running Python script:

    #test.py
    i = 0
    while True:
        i += 1
    

    I'm able to successfully run the script using SysProcessExecuteCommand2 (I can see the Python process running in the task manager). However, this "freezes" the Codesys execution of the task - it sits on that command and will not continue until the Python script ends. I've proven this works by using the task monitor to kill the process, then Codesys continues on happily through the task.

    What I want to do is use SysProcessCreate2 to set up the Python script as a background task and monitor the process using SysProcessGetState. This always results in the error "ERR_OPERATION_DENIED". I know that I'm able to execute commands (as above), so my config should be correct. Is SysProcessCreate2 functional on a Linux environment? Is there something in the config file I'm missing?

    Here's my "working" code that uses SysProcessExecuteCommand2:

    VAR
        bTest : BOOL;   
        sCommand : STRING := '/usr/bin/python3 /var/opt/codesys/PlcLogic/Application/test.py';
        refCommand : REFERENCE TO STRING REF= sCommand;
        sOutput : STRING;
        refOutput : REFERENCE TO STRING REF= sOutput;
        result : RTS_IEC_RESULT;
    END_VAR
    
    IF bTest THEN
        SysProcessExecuteCommand2(
            pszCommand:= refCommand,
            pszStdOut:= refOutput,
            udiStdOutLen:= SIZEOF(sOutput),
            pResult:= ADR(result)
        );  
        bTest:= FALSE;
    END_IF
    

    And here's the code that always results in the ERR_OPERATION_DENIED error:

    VAR
        bTest : BOOL;   
        sApp : STRING := '/usr/bin/python3';
        sCommand : STRING := '/var/opt/codesys/PlcLogic/Application/test.py';
        refApp : REFERENCE TO STRING REF= sApp; 
        refCommand : REFERENCE TO STRING REF= sCommand; 
        hResult : SysProcess.RTS_IEC_RESULT;
        hHandle : SysProcess.RTS_IEC_HANDLE;
    END_VAR
    
    IF bTest THEN
        hHandle := SysProcessCreate2(
            pszApplication:= refApp,
            pszCommandLine:= refCommand,
            ulFlags:=SYSPROCESS_CREATEFLAG_HIDDEN,
            pResult:= ADR(hResult)
        );  
        bTest:= FALSE;
    END_IF
    

    Here's the relevant section from the config file at /etc/codesyscontrol/CODESYSControl.cfg:

    [SysProcess]
    Command=AllowAll
    

    Appreciate any support or ideas. Thanks!

     

    Last edit: trusty-squire 2025-03-16

Log in to post a comment.