title: How to find exit code or reason when atexit callback is called in Python?
body: <p>I want to know if a Python script is terminating correctly or not. For this I am using <a href="http://docs.python.org/library/atexit.html" rel="noreferrer"><code>atexit</code></a> but the problem is that I do not know how to differentiate if atexit was called with sys.exit(0) or non zero or an exception.</p>
  
  <p>Reasoning: if program ends properly, it will do nothing but if the program ends by an exception or returning an error code (exit status) different than zero I want to trigger some action.</p>
  
  <p>In case you will wonder why I'm not using try/finally is because I want to add the same behaviour for a dozen of scripts that are importing a common module. Instead of modifying all of them, I want to add the atexit() hack to the module being imported and get this behaviour for free in all of them.</p>