• Recent
    • Tags
    • Popular
    • Register
    • Login

    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 Mango 5 Documentation Website

    Invoke shell commands from a Scripting environment?

    Scripting general Discussion
    3
    8
    1.9k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • phildunlapP
      phildunlap
      last edited by phildunlap

      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 );
      1 Reply Last reply Reply Quote 0
      • MattFoxM MattFox referenced this topic on
      • Jdiaz-coJ
        Jdiaz-co
        last edited by

        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?

        MattFoxM 1 Reply Last reply Reply Quote 0
        • MattFoxM
          MattFox @Jdiaz-co
          last edited by

          @Jdiaz-co add the java opts to start-options.sh in the mango-data directory.
          The rest should be the same

          Fox

          Do not follow where the path may lead; go instead where there is no path.
          And leave a trail - Muriel Strode

          Jdiaz-coJ 1 Reply Last reply Reply Quote 0
          • Jdiaz-coJ
            Jdiaz-co @MattFox
            last edited by

            @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.

            MattFoxM 1 Reply Last reply Reply Quote 0
            • MattFoxM
              MattFox @Jdiaz-co
              last edited by

              @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
              add JAVAOPTS="$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

              Do not follow where the path may lead; go instead where there is no path.
              And leave a trail - Muriel Strode

              Jdiaz-coJ 2 Replies Last reply Reply Quote 0
              • Jdiaz-coJ
                Jdiaz-co @MattFox
                last edited by

                @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.

                1 Reply Last reply Reply Quote 0
                • Jdiaz-coJ
                  Jdiaz-co @MattFox
                  last edited by

                  @MattFox Alright, in Mango v5, the variable should be set as follows: MA_JAVA_OPTS="$JAVAOPTS -Dnashorn.args=-scripting". Thanks for your help

                  MattFoxM 1 Reply Last reply Reply Quote 0
                  • MattFoxM
                    MattFox @Jdiaz-co
                    last edited by

                    @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

                    Do not follow where the path may lead; go instead where there is no path.
                    And leave a trail - Muriel Strode

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post