Best way to fail Python script to flag an error from QSHELL.

Issue #123 resolved
Jason Olson created an issue

So in one of the Python processes we have we connect to an outside service. They provide us transaction details and I pull them in and write them to a table on IBM i. We call this process using QSHELL and if basic things like the directory isn’t there or the Python script isn’t there will trigger a failure in QSHELL, but if I’m inside the Python script I can detect errors, and write them to log files, but I can’t figure out a way for an error inside the script to cause a failure in QSHELL.

How would this normally be handled?

Jason

Comments (6)

  1. Jesse G

    Greetings. What exactly are you looking for to “trigger a failure”?
    If you mean that you don’t want QSH ended normally with exit code 0, then you can just monitor the return code of the python3 command and return non-zero if the python command returns non-zero. In QSH, you can access a processes exit code with $? (which will give you the exit code of the previously-run process). So you could simply call sys.exit() from your Python code and catch that in a surrounding QSH script (if there is one). Or, if python is the last thing in the script, you can simply run it with exec. For instance, instead of python3 myprog.py, run exec python3 myprog.py

  2. Jason Olson reporter

    OK I think I missed the boat. What I’m looking for is if an error occurs INSIDE the Python script, I want that to trigger an error in QSHELL. So instead of getting QSH ended normally with exit code 0 it would give me exit code 1 or 2 or whatever.

    Currently any failures inside the Python script I can log, but I can’t pass the error back to QSHELL to trigger an exit code of 1 or 2.

    Jason

  3. Log in to comment