Best way to fail Python script to flag an error from QSHELL.
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)
-
-
FYI, this doc may be helpful: https://www.ibm.com/support/pages/using-rcvmsg-and-monmsg-retrieve-qshell-messages
-
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
-
Jason, see the link I posted earlier. You need to set QIBM_QSH_CMD_ESCAPE_MSG environment variable to Y before calling QSH. This causes an escape message to be sent if the return code is non-zero. You can call https://docs.python.org/3.8/library/sys.html#sys.exit to set the return code. Any uncaught exception should trigger a non-zero return code IIRC.
-
@Jason Olson , is this still unresolved?
-
- changed status to resolved
Closing due to lack of feedback
- Log in to comment
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 thepython3
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 callsys.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 ofpython3 myprog.py
, runexec python3 myprog.py