Ada*_*eda 4 groovy jenkins jenkins-groovy
我正在通过系统 Groovy 脚本运行 shell 命令。这是一个漫长的运行过程,大约 30 分钟,而且相当冗长。但正如所写,这会在打印 stdout 或 stderr 之前等待命令完成。有没有办法在脚本执行时将输出发送到控制台(就像在终端会话中一样)。
def sout = new StringBuffer(), serr = new StringBuffer()
def proc = "/some/long/running/command".execute()
proc.consumeProcessOutput(sout, serr)
println "STDOUT\n $sout"
println "STDERR\n $serr"
Run Code Online (Sandbox Code Playgroud)
更新:这是我根据答案尝试的代码。在进程完成或被终止之前,它不会打印任何内容。
def cmd = "/home/adam/test.sh"
StringBuffer sout = new StringBuffer()
StringBuffer serr = new StringBuffer()
def process = cmd.execute()
process.waitForProcessOutput sout, serr
sout.each
println "Line ${it}"
}
Run Code Online (Sandbox Code Playgroud)
虽然您找到的解决方案可能会正常工作,但我认为最好使用waitForProcessOutput which also allows to stream the error output of the process.
完整的解决方案将是这样的:
#!/usr/bin/env groovy
Process proc = "ping -c 10 google.com".execute()
proc.waitForProcessOutput(System.out, System.err)
System.exit(proc.exitValue())
Run Code Online (Sandbox Code Playgroud)
例如,此方法有多种替代方法,可以仅捕获正常输出或仅捕获错误输出。你可以在这里找到它们.
Groovy 文档中还有一个(相当短的)章节介绍了如何执行外部进程。
| 归档时间: |
|
| 查看次数: |
5083 次 |
| 最近记录: |