2

I have the following code:

try:
    self._collect_persons_status()
except manager.AsteriskManagerError:
    # If it is not possible to continue with the collection of initial
    # person statuses via Asterisk Manager, end the program with an
    # error code.
    logger.debug('*** exit with 1!!')
    sys.exit(1)

This script is handled via systemd (runs as a daemon with a loop).

From the log I can see that *** exit with 1!! is printed, but the script ends with code 0, not 1 as expected:

script-exit-code

What am I doing wrong?

2
  • 3
    Do you perhaps have try: <invoke script entry point> except: <log error> at the script top-level? Commented Jul 20, 2020 at 8:41
  • @user4815162342 oh yes, you're right! If you post this as an answer I'll give you the best check Commented Jul 20, 2020 at 8:45

1 Answer 1

6

If you have something like this at script top-level, it will prevent sys.exit() from exiting the program:

try:
    <invoke script entry point>
except:
    <log the error>

This is because sys.exit() is implemented by raising the SystemExit exception. You can fix the code by changing except: to except Exception which won't catch SystemExit (and some other low-level exceptions that you probably don't want to handle either).

Sign up to request clarification or add additional context in comments.

2 Comments

I would use except Exception, which will not catch exiting exceptions.
@timgeb Good idea; I've now amended the answer to recommend that instead of explicit except SystemExit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.