Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy script not able to execute external process

I'm trying to execute a python external process from my Groovy script but it doesn't produce any output.

So as a small sanity test I tried simply outputting the python version:

    def command = """ /usr/local/bin/python -V """
    def proc = command.execute()
    proc.waitFor()
    println "This is output: " + proc?.in?.text

The above doesn't produce any output, however, from my command line I AM able to run /usr/local/bin/python -V

What is weird is that if I modify the script to run identify then it does produce an output.

    def command = """ /usr/local/bin/identify --version """
    def proc = command.execute()
    proc.waitFor()
    println "This is output: " + proc?.in?.text

What can be causing this behavior?

like image 827
Anthony Avatar asked Jan 25 '26 04:01

Anthony


1 Answers

python -V command prints the version number to standard error instead of standard output.

$ python -V
Python 2.7.8
$ python -V 2>/dev/null
$

So, to get the output, redirect the stderr (2) to stdout (1):

def command = """ /usr/local/bin/python -V 2>&1 """
def proc = command.execute()
proc.waitFor()
println "This is output: " + proc?.in?.text

Alternatively, you can use err instead of in to get standard error output:

def command = """ /usr/bin/python -V """
def proc = command.execute()
proc.waitFor()
println "This is output: " + proc?.err?.text
like image 86
falsetru Avatar answered Jan 26 '26 17:01

falsetru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!