def torun = "ls -l "
proc = torun.execute()
proc.waitFor()
This will hang on UNIX systems if these programs generate more STDOUT data than the buffer can handle. Better to handle this yourself with the following:
#!/usr/bin/env groovy
void execute(def command ) {
println "Running command : " + command
ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c", command)
pb.redirectErrorStream(true)
def process = pb.start()
def output = process.inputStream
output.each() { // Throw away stdout in this example, could return it at the end.
}
process.waitFor()
println "Done executing command , return value : " + process.exitValue()
process.destroy()
process = null
pb = null
System.gc()
}
execute("ls -l /usr/bin ")
NOTE From: http://jira.codehaus.org/browse/GROOVY-2620
A Better solution is :
StringWriter stringWriterOutput = new StringWriter()
StringWriter stringWriterError = new StringWriter()
Process proc = command.execute()
stringWriterOutput << proc.in
stringWriterError << proc.err
proc.waitFor()

0 comments:
Post a Comment