Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An error equivalent for process.text?

Tags:

groovy

You can get the entire output stream by using .text:

def process = "ls -l".execute()
println "Found text ${process.text}"

Is there a concise equivalent to get the error stream?

like image 359
ripper234 Avatar asked May 21 '12 16:05

ripper234


2 Answers

You can use waitForProcessOutput which takes two Appendables (docs here)

def process = "ls -l".execute()
def (output, error) = new StringWriter().with { o -> // For the output
  new StringWriter().with { e ->                     // For the error stream
    process.waitForProcessOutput( o, e )
    [ o, e ]*.toString()                             // Return them both
  }
}
// And print them out...
println "OUT: $output"
println "ERR: $error"
like image 181
tim_yates Avatar answered Oct 10 '22 15:10

tim_yates


Based on tim_yates answer, I tried it on Jenkins and found this issue with multiple assignment: https://issues.jenkins-ci.org/browse/JENKINS-45575

So this works and it is also concise:

def process = "ls -l".execute()
def output = new StringWriter(), error = new StringWriter()
process.waitForProcessOutput(output, error)
println "exit value=${process.exitValue()}"
println "OUT: $output"
println "ERR: $error"
like image 34
Boris Lopez Avatar answered Oct 10 '22 14:10

Boris Lopez