linux - 在 Linux shell 上使用 groovy 执行长命令?

标签 linux bash shell groovy sh

如何使用 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/

相关文章:

linux - 在文件名中调用时 Shell 变量为空,但在回显或在其他地方调用时则不是

linux - 每 30 秒进行一次日志轮换并将日志文件存储在以日期命名的目录中

c++ - 为什么这个库 dlopen 顺序很重要?

c++ - Mongodb : error while loading shared libraries: libboost_thread. so.1.54.0: > 无法打开共享对象文件:没有这样的文件或目录

bash - 如何在 bash curl 命令中使用提示中的变量?

bash - 在 Bash IO 重定向中等待 subshel​​l

linux - bash 脚本计算错误

java - 如何从 Java 代码运行 Unix shell 脚本?

linux - ssh 端口转发 ("ssh -fNL") 无法通过 expect spawn 自动提供密码

linux - Linux中使用管道进行进程间通信