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-8What 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 );
-
-
Good morning, I have tried to implement this solution in Mango v5.1.4, however, I find that this folder does not exist in your installation. Could you please tell me how to do it for that version?
-
@Jdiaz-co add the java opts to start-options.sh in the mango-data directory.
The rest should be the sameFox
-
@MattFox In the v5 installation, there are no "ext-enabled" or "ext-available" folders, so I don't know where to place the scripts you mentioned as a solution.
-
@Jdiaz-co I never mentioned ext enabled. Form mango4 + mango uses start-options.sh
navigate to your mango 5 installation and there should be a mango-data directory.
Inside there is a start options.sh file
addJAVAOPTS="$JAVAOPTS -Dnashorn.args=-scripting"
to a new line in the file.once you've added it, restart mango then you can use the $EXEC command
Fox
-
@MattFox said in Invoke shell commands from a Scripting environment?:
JAVAOPTS="$JAVAOPTS -Dnashorn.args=-scripting"
That's what I did, however, I still can't use the exec command. In Mango v3, it works for me, but in Mango v5, it doesn't.
-
@MattFox Alright, in Mango v5, the variable should be set as follows: MA_JAVA_OPTS="$JAVAOPTS -Dnashorn.args=-scripting". Thanks for your help
-
@Jdiaz-co
MA_JAVA_OPTS="$MA_JAVA_OPTS -Dnashorn.args=-scripting"
will be the ticket then
I see they've changed it a fair bit since then, I'm not near an install right this minute to compare
Fox