title: What's a good way to detect when a Python program exits or crashes?
body: <p>I have the following Python program running in a Docker container. </p>
  
  <p>Basically, if the Python process exits gracefully (ex. when I manually stop the container) or if the Python process crashes (while inside <code>some_other_module.do_work()</code>) then I need to do some cleanup and ping my DB telling it that process has exited. </p>
  
  <p>What's the best way to accomplish this? I saw one answer where they did a try catch on <code>main()</code>, but that seems a bit odd.</p>
  
  <p>My code:</p>
  
  <pre><code>def main():
      some_other_module.do_work()
  
  if __name__ == '__main__':
      main()
  </code></pre>