How to find exit code or reason when atexit callback is called in Python?

You can solve this using sys.excepthook and by monkey-patching sys.exit(): import atexit import sys class ExitHooks(object): def __init__(self): self.exit_code = None self.exception = None def hook(self): self._orig_exit = sys.exit sys.exit = self.exit sys.excepthook = self.exc_handler def exit(self, code=0): self.exit_code = code self._orig_exit(code) def exc_handler(self, exc_type, exc, *args): self.exception = exc hooks = ExitHooks() hooks.hook() def … Read more