如何使用 groovy 执行长命令? 当我在 Linux 终端上执行此命令时,我得到了所需进程的正确进程 ID。使用 groovy 我得到一个空的返回。这是我已经尝试过的:
def p = "ps aux | grep 'unit 1' | grep -v grep | awk '{print $2}'".execute()
p.waitFor()
println p.text // this is empty, but it shouldn't
groovy 的正确执行应该是什么样的?如何在 groovy 中获取正确的进程 ID?
最佳答案
这有点重复评论中的两个问题(加上我目前找不到的其他一些问题),但请尝试:
def cmd = /ps aux | grep 'unit 1' | grep -v grep | awk '{print $2}'/
def out = [ '/bin/sh', '-c', cmd ].execute().text.trim()
println out
关于linux - 在 Linux shell 上使用 groovy 执行长命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22134287/