打印groovy过程中发生的所有事情
请帮助我
这是我的代码
try {
println "[${LocalTime.now()}] Start..."
def proc =["/bin/sh", "-c","./commandx"].execute()
proc.waitForOrKill(420 * 1000)
def output = proc.text?.trim()
println "[${LocalTime.now()}] Output = ${output}"
} catch (IOException e) {
System.err.println("[${LocalTime.now()}] Process killed before completing! 6")
} finally {
println "[${LocalTime.now()}] End..."
}
答案 0 :(得分:0)
我认为您应该使用waitForProcessOutput
:
从进程获取输出和错误流,并读取它们,以防止进程由于输出缓冲区已满而阻塞。处理后的流数据将附加到提供的OutputStream中。为此,启动了两个线程,但加入了join(),因此我们等待。正如waitFor ...名称所暗示的,我们也要等到完成为止。最后,关闭输入,输出和错误流。
所以我想这是您要查找的代码:
try {
println "[${LocalTime.now()}] Start..."
def proc =["/bin/sh", "-c", "./commandx"].execute()
proc.waitForOrKill(420 * 1000)
proc.waitForProcessOutput(System.out, System.err)
} catch (IOException e) {
System.err.println("[${LocalTime.now()}] Process killed before completing! 6")
} finally {
println "[${LocalTime.now()}] End..."
}