Executing External Processes From Grails

Posted: Thu, Mar 10 04:12 AM (PST)

Simple exec method as shown below to run sub processes from groovy / grails. If you run the standard exec like:

def cmd = "/bin/ls -la"
def proc = cmd.execute()
proc.waitFor()

But I have other advanced solution for you. Where you can read response from your executed program. See below:

def command = "/bin/ls -la"
StringWriter stringWriterOutput = new StringWriter()
StringWriter stringWriterError = new StringWriter()
Process proc = command.execute()
stringWriterOutput << proc.in
stringWriterError << proc.err
proc.waitFor()
Tags: