Network Change & Configuration Management

Reply
Highlighted
Accepted Solution

convincing a Python script to exit with an error status

AndrewSJ
Techie
Posts: 10
207     0

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.

Re: convincing a Python script to exit with an error status

skuchynski
Techie
Posts: 8
208     0

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

Re: convincing a Python script to exit with an error status

AndrewSJ
Techie
Posts: 10
208     0

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.

Re: convincing a Python script to exit with an error status

AndrewSJ
Techie
Posts: 10
208     0

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)

sys.exit()

Showing results for 
Search instead for 
Do you mean 

Recommended for You