Please Note This forum exists for community support for the Mango product family and the Radix IoT Platform. Although Radix IoT employees participate in this forum from time to time, there is no guarantee of a response to anything posted here, nor can Radix IoT, LLC guarantee the accuracy of any information expressed or conveyed. Specific project questions from customers with active support contracts are asked to send requests to support@radixiot.com.

Radix IoT Website Mango 3 Documentation Website Mango 4 Documentation Website

Invoke shell commands from a Scripting environment?


  • I received a question over the phone of if it were possible to invoke shell commands in an easy synchronous manner from a scripting environment. To that, I have this answer:

    Yes, it is very easy to get synchronous shell calling in a Mango JavaScript context. While we haven't added the ability to toggle this only on a per-script basis, you can have a script in Mango/bin/ext-enabled like

    nashorn-scripting.bat

    if "%1" == "init" (
            rem Set the -scripting flag for shell extensions in JavaScript environments
            set JAVAOPTS=%JAVAOPTS% -Dnashorn.args=-scripting
    )
    

    nashorn-scripting.sh

    case "$1" in
        init)
            # Set the -scripting flag for shell extensions in JavaScript environments
            JAVAOPTS="$JAVAOPTS -Dnashorn.args=-scripting"
            ;;
    esac
    

    Important to remember "init" is called on a stop, then start, but not on a restart (which calls the scripts with "restart")

    Which will give us the $EXEC option an the option to use backtick syntax, as well as several other utilities, expanded upon here: https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/shell.html or perhaps:
    https://www.google.com/search?q=nashorn+scripting+mode+extensions&oq=nashorn+scripting+mode+extensions&aqs=chrome..69i57.4663j1j7&sourceid=chrome&ie=UTF-8

    What this will enable us to do in a Mango script is:

    //Linux only, Windows may have PATH issues.
    print(`echo "Wow that's awesome!"`);
    print($EXEC('echo "Alternative syntax?!?"'));
    

    It would also be possible for someone to invoke command line functions without toggling on the -scripting nashorn mode, using

    //Asynchronously kick off the command
    com.serotonin.m2m2.rt.maint.work.ProcessWorkItem.queueProcess("command", timeout);
    //Synchronously kick off the command, get an object with the command and output back
    var response = com.serotonin.m2m2.rt.maint.work.ProcessWorkItem.executeProcessCommand("command", timeout);
    print( response.key );
    print( response.value );