Jump to content
Server Maintenance This Week. ×

RunCommand module , I wrote


This topic is 489 days old. Please don't post here. Open a new topic instead.

Recommended Posts

  • Newbies

Hi,
A Christmas gift.

With minor improvements to "RunShellScript" module in ScriptMaster_16+ sample,
I have wriiten "RunCommand" module.

With a parameter "command" that cantains command and its parameters delimited by Filemaker newline
"¶" s.
, the function run the any command that can run at your computer and get the stdout strings as a return code

examle(case of widows os):
  set variable [ $a = RunCommand( "tasklist¶/nh" ) ] 
or
    set variable [ $a = RunCommand( "cmd¶/c¶dir" ) ]

I use only Windows machines, but perhaps this module will work in Mac environment
You can freely use it at your own risk.

Thank you

source is here
------------------------------------------------------------------------------
String delimitter = '\\u000a';
String encode = "shift-jis";

String[] cmds=command.split( delimitter );
boolean waitForOutput = true;
timeout = 0;
//cmds = [command, params];

StringBuffer stdout;
InputStream inputStream;
Process process;

ProcessBuilder procBuilder = new ProcessBuilder(cmds);
procBuilder.redirectErrorStream(true);
process = procBuilder.start();

inputStream = process.getInputStream();
stdout = new StringBuffer();


if( Boolean.valueOf( waitForOutput ) ) { //Wait for command to finish

    Thread stdoutThread = new Thread( "stdout reader" ) {
        public void run() {
            try {
                Reader r = new InputStreamReader( inputStream, encode );
                char[] buff = new char[1024];
                int charsRead;
                while( ( charsRead = r.read( buff ) ) != -1 ) {
                    stdout.append( buff, 0, charsRead );
                }
            } catch( IOException e ) {
                throw new RuntimeException( e );
            } catch ( InterruptedException e) {
                throw new RuntimeException ( e );
            }
        }
    };
    
    try {
        stdoutThread.start();
    }catch (RuntimeException e) {
        throw e;
    }


    final int timeoutMilliseconds = Integer.valueOf( timeout ) * 1000;
    if( timeoutMilliseconds > 0 ) {
        final Thread mainThread = Thread.currentThread();
        new Thread("timeout thread") {
            public void run() {
                try {
                    Thread.sleep( timeoutMilliseconds );
                    process.destroy();
                    Thread.interrupt();
                    mainThread.interrupt();
                    
                } catch( InterruptedException e ) {
                    //process.destroy();
                    throw e;
                }
            }
        }.start();
    }
    try {
        if (process.waitFor() == 0) {
            stdoutThread.join(); //Wait for all output to be read
            return stdout.toString();
        }
        else {
            stdoutThread.join();
            //return stdout.toString();
            throw new RuntimeException("Process was terminated.");
        }
    } catch( InterruptedException e ) {
        throw new RuntimeException("Process was interrupted; error output is: " );
    } catch( RuntimeException e ) {
        //return stdout.toString();
        throw e;
 }
} else { //Don't wait, return immediately
    inputStream.close();
    return "Executed shell command: " + command;
}
----------------------------------------------------------------------
You can change the encode in 2nd line that you like.

Link to comment
Share on other sites

This topic is 489 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.