gpt4 book ai didi

groovy - 在Groovy中捕获过程输出

转载 作者:行者123 更新时间:2023-12-03 14:42:29 25 4
gpt4 key购买 nike

我有一个Groovy脚本,该脚本遍历目录以查找.png文件,并在每个目录上调用pngquant(命令行实用程序)。 pngquant的输出应打印在终端上。相关代码为:

def command = "pngquant -f -ext .png"

root.eachFileRecurse(groovy.io.FileType.FILES) {File file ->

if (file.name.endsWith('.png')) {
println "Compressing file: $file"

def imgCommand = "$command $file.absolutePath"

Process pngquantCmd = imgCommand.execute()
pngquantCmd.consumeProcessOutput(System.out, System.err)
}
}


该脚本可以正常工作,但是一旦处理完所有文件,似乎仍在重定向粗壮,因为除非使用Ctrl + C终止该进程,否则命令提示符永远不会出现。是否需要以某种方式“撤消”

pngquantCmd.consumeProcessOutput(System.out, System.err)        


还是有更好的方法将此过程的输出重定向到控制台?我想我可以通过添加 System.exit(0)来解决此问题,但这似乎不是正确的解决方案。该问题仅在Linux上发生。

最佳答案

代替

    pngquantCmd.consumeProcessOutput(System.out, System.err)        


无论进程的情况如何,这都会启动几个线程来读取输出并犁开,您应该尝试

    pngquantCmd.waitForProcessOutput(System.out, System.err)


这将重定向过程输出,然后等待其完成,然后继续:-)

关于groovy - 在Groovy中捕获过程输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11579740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com