Capture shell output after running python script that raised Exception

Question:

I am running a shell in groovy, that executes a python script. I was able to get the out put of the python script by assigning it to a variable.
However, i would like to capture Exceptions as well, but in case of exception i am unable to get the python trace.
the groovy script:

script{
       try{
             var_name = bash("""python3 some_script.py""")
             echo "$(var_name)"
           }
       catch(exc){
            echo "$(var_name)"
                  }
       }

in case of no exception raised from python, output from script is printed, but in case of exception in prints null.
any solutions?

Asked By: Yakov Abrams

||

Answers:

Python writes exceptions to standard error, hence if you want to capture them along with the normal output, you should redirect stderr to stdout by adding 2>&1 to the bash input.

In your case it would be bash("""python3 some_script.py 2>&1""").

I assume the bash function sends the argument to the bash shell.

Answered By: Dzienny
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.