12-07-2017 11:51 AM
Has anyone found a way to have a Python script running on a NetMRI appliance exit with an error status? I have a Python script that does some input validation. If the user doesn't type valid values into the available text fields, I want the script to terminate with an error status. Getting the script to terminate is no problem, but it always shows up with an OK status in the Job History tab in NetMRI. I've tried using sys.exit(1) and os._exit(1) but to no avail.
I might mention that in a Perl script, using exit(1); causes the script to exit with an Error status on the Job History tab. I'm just looking for the same behaviour in my Python script.
Solved! Go to Solution.
12-08-2017 02:58 AM
You are right, exiting with error code should be considered as a job error and it looks to be a bug. There is a workaround though that might work for you - just print your error message to STDERR. It should mark job as failed and be printed in job logs
12-12-2017 07:09 AM
I gave your suggestion a try and, sure enough, I can force a Python script to exit with an error status using a line like this:
print('error message goes here', file=sys.stderr)
The error message is printed on the Status Log tab of the Job Details Viewer window.
Thanks for the tip.
12-12-2017 07:26 AM
I should clarify you still need to use sys.exit() to actually terminate the script. Just using the print statement forces an error status but the script won't exit at that point and NetMRI will happily execute the rest of the script. So use the two lines together if you want the script to terminate immediately.
print('error message', file=sys.stderr)
07-03-2018 12:01 PM - edited 07-03-2018 12:02 PM
The upcoming webinar - A Python Primer for NetMRI - would be a great opportunity to ask more questions on this subject.
It is on July 19 at 10 AM PDT.
Check out our new Tech docs website for latest documentation on Infoblox products.