我需要在一个Jenkins'slaves'脚本控制台上运行一些任意的东西,比如python -c "print('hello')"
。以下是我正在尝试的:
def cmd = 'python -c "print(\'hello\')"'
def sout = new StringBuffer(), serr = new StringBuffer()
def proc = cmd.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
println "out> $sout\nerr> $serr"
但是,获得空输出:
out>
err>
有没有办法在Groovy中获取python的输出?
这对我来说是完美的:
使用“Execute Groovy script”(而不是“Execute system Groovy script”)
Groovy执行shell&python命令
要在上述答案中添加一个更重要的信息,请考虑正在执行的python命令或脚本的
stdout
和stderr
。Groovy添加了
execute
方法,使执行shell相当容易,例如:python -c
命令:但是,如果您希望获得与cmd标准输出相关的
String
(stdout
)和/或标准错误(stderr
),则上述代码不会产生结果输出。因此,为了获得Groovy exec进程的cmd输出,请始终尝试使用:
而不是
通过这种方式,我们在Groovy中执行命令后捕获输出,因为后者是一个阻塞调用(check ref for reason)。
完整示例w/
executeBashCommand
func输出
注意1:如上面的代码(
bashCmd2
)示例所示,对于更复杂的python脚本,您应该通过.sh
bash shell脚本执行它。注2:所有示例都在
尝试将命令分成数组
不知道为什么你的版本不起作用。
相关问题 更多 >
编程相关推荐