You can get the entire output stream bằng cách sử dụng .text:Lỗi tương đương với process.text?
def process = "ls -l".execute()
println "Found text ${process.text}"
Có tương đương súc tích để có được những dòng lỗi?
You can get the entire output stream bằng cách sử dụng .text:Lỗi tương đương với process.text?
def process = "ls -l".execute()
println "Found text ${process.text}"
Có tương đương súc tích để có được những dòng lỗi?
Bạn có thể sử dụng waitForProcessOutput
mà phải mất hai 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"
Dựa trên tim_yates trả lời, tôi đã cố gắng nó trên Jenkins và thấy vấn đề này với nhiều công việc: https://issues.jenkins-ci.org/browse/JENKINS-45575
Vì vậy, các công trình này và nó cũng ngắn gọn:
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"
Tôi sẽ chấp nhận câu trả lời của bạn vì đó là câu trả lời ngắn gọn nhất. Tôi tự hỏi tại sao không có 'process.error'. – ripper234
chủ yếu là do sử dụng thành viên .text là nguy hiểm. Nếu văn bản xuất ra hoặc luồng đầu ra hoặc lỗi vượt quá vùng đệm, thì quá trình của bạn sẽ tạm dừng cho đến khi một số luồng được đọc. Nó thực sự là một ý tưởng tốt khi bạn không biết bao lâu đầu ra sẽ được (mà thường là trường hợp cho các lỗi) để sử dụng một thread riêng biệt để nắm bắt các dòng. – billjamesdev
+1 cho nhận xét @BillJames. Ngoài ra, đây là những gì ['waitForProcessOutput' thực hiện] (https://github.com/groovy/groovy-core/blob/master/src/main/org/codehaus/groovy/runtime/ProcessGroovyMethods.java#L206) (tạo hai luồng để nghe đầu ra) –