Call function the moment before script crashes?
<p>I am solving the following problem:</p>

<p>I have a script which should be running as a service 24/7. Because it is quite important, I would like it to send me an email in case it crashes. I found <a href="https://docs.python.org/3/library/atexit.html" rel="nofollow">atexit</a> module, but the docs specifically says it cannot handle crashes.</p>

<p>So my question is: is there any better way of achieving this than by running another serivce which will be checking if this one is running? Or the goal here is to write the service so that it cannot crash (which is almost impossible)?</p>

<p>Thanks for your ideas!</p>

<p>I mean to do what you ask in the first part just wrap the main part of the script with a try/except.</p>

<p>myscript.py:</p>

<pre><code>def main():
   long_running_process()

if __name__ == '__main__':
   try:
       main()
   except:
       send_email()
</code></pre>

python crash crash-reports