How to make a groovy tag?

I like to execute the command and copy its stdout and stderr to the log file.

I like to run:

p = myexecute("ls -l /tmp/")

And essentially, the same process object returns as:

p = "ls -l /tmp/".execute()

The difference is that stdout / stderr is copied to the specified log file. I'm sure there is an easy way to do this in groovy, but I'm not groovy enough to see it.

+3
source share
3 answers

A better solution might be:

def logFile = new File( '/tmp/log.txt' )
logFile.withWriter('UTF-8') { sout ->
    p = "ls -l /tmp/".execute()
    p.waitForProcessOutput(sout, sout)
}

Since it will wait for the process to complete

+1
source

This answer is long overdue, but one solution might be:

import groovy.ui.SystemOutputInterceptor

File logFile = new File('stdout.log')                                                                                
new SystemOutputInterceptor({logFile << it; true}).start() 
...
+1
source

You are right, it is simple:

logFile.withWriter('UTF-8') { sout ->
    p = "ls -l /tmp/".execute()
    p.consumeProcessOutput(sout, sout)
}

This will write both streams to the same file.

For more examples, see http://groovy.codehaus.org/Process+Management

0
source

All Articles