I'm trying to create a method in Groovy to get the Process ID of an application. I'm currently at this stage:
String getProcessIdFor(String program) {
def buffer = new StringBuffer()
Process commandOne = 'ps -A'.execute()
Process commandTwo = "grep -m1 '${program}'".execute()
Process commandThree = "awk '{print \$1}'".execute()
Process process = commandOne | commandTwo | commandThree
process.waitForProcessOutput(buffer, buffer)
return buffer.toString()
}
But this gives me:
Exception in thread "Thread-1" groovy.lang.GroovyRuntimeException: exception while reading process stream
awk: syntax error at source line 1
at org.codehaus.groovy.runtime.ProcessGroovyMethods$3.run(ProcessGroovyMethods.java:402)
context is
at java.lang.Thread.run(Thread.java:745)
>>> ' <<<
missing }
Caused by: java.io.IOException: Stream closed
awk: bailing out at source line 1
at java.lang.ProcessBuilder$NullOutputStream.write(ProcessBuilder.java:434)
at java.io.OutputStream.write(OutputStream.java:116)
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:122)
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:122)
at org.codehaus.groovy.runtime.ProcessGroovyMethods$3.run(ProcessGroovyMethods.java:399)
... 1 more
Process finished with exit code 0
It looks like it's struggling on the awk
command, but I can't seem to figure out where I'm going wrong. Any ideas?